Index: new_tests/Makefile ================================================================== --- new_tests/Makefile +++ new_tests/Makefile @@ -22,10 +22,11 @@ OFConcreteMutableArrayTests.m \ OFConcreteMutableDictionaryTests.m \ OFConcreteMutableSetTests.m \ OFConcreteSetTests.m \ OFCryptographicHashTests.m \ + OFDataTests.m \ OFDateTests.m \ OFDictionaryTests.m \ OFHMACTests.m \ OFINIFileTests.m \ OFIRITests.m \ @@ -34,10 +35,11 @@ OFListTests.m \ OFLocaleTests.m \ OFMatrix4x4Tests.m \ OFMethodSignatureTests.m \ OFMutableArrayTests.m \ + OFMutableDataTests.m \ OFMutableDictionaryTests.m \ OFMutableSetTests.m \ OFMutableStringTests.m \ OFMutableUTF8StringTests.m \ OFNotificationCenterTests.m \ ADDED new_tests/OFDataTests.h Index: new_tests/OFDataTests.h ================================================================== --- new_tests/OFDataTests.h +++ new_tests/OFDataTests.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2008-2024 Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFDataTests: OTTestCase +{ + OFData *_data; + unsigned char _items[2][4096]; +} + +@property (readonly, nonatomic) Class dataClass; +@end ADDED new_tests/OFDataTests.m Index: new_tests/OFDataTests.m ================================================================== --- new_tests/OFDataTests.m +++ new_tests/OFDataTests.m @@ -0,0 +1,291 @@ +/* + * Copyright (c) 2008-2024 Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#include + +#import "OFDataTests.h" + +@implementation OFDataTests +- (Class)dataClass +{ + return [OFData class]; +} + +- (void)setUp +{ + [super setUp]; + + memset(&_items[0], 0xFF, 4096); + memset(&_items[1], 0x42, 4096); + + _data = [[self.dataClass alloc] initWithItems: _items + count: 2 + itemSize: 4096]; +} + +- (void)dealloc +{ + [_data release]; + + [super dealloc]; +} + +- (void)testCount +{ + OTAssertEqual(_data.count, 2); +} + +- (void)testItemSize +{ + OTAssertEqual(_data.itemSize, 4096); +} + +- (void)testItems +{ + OTAssertEqual(memcmp(_data.items, _items, 2 * _data.itemSize), 0); +} + +- (void)testItemAtIndex +{ + OTAssertEqual( + memcmp([_data itemAtIndex: 1], &_items[1], _data.itemSize), 0); +} + +- (void)testItemAtIndexThrowsOnOutOfRangeIndex +{ + OTAssertThrowsSpecific([_data itemAtIndex: _data.count], + OFOutOfRangeException); +} + +- (void)testFirstItem +{ + OTAssertEqual(memcmp(_data.firstItem, &_items[0], _data.itemSize), 0); +} + +- (void)testLastItem +{ + OTAssertEqual(memcmp(_data.lastItem, &_items[1], _data.itemSize), 0); +} + +- (void)testIsEqual +{ + OTAssertEqualObjects( + _data, [OFData dataWithItems: _items count: 2 itemSize: 4096]); + OTAssertNotEqualObjects( + _data, [OFData dataWithItems: _items count: 1 itemSize: 4096]); +} + +- (void)testHash +{ + OTAssertEqual(_data.hash, + [[OFData dataWithItems: _items count: 2 itemSize: 4096] hash]); + OTAssertNotEqual(_data.hash, + [[OFData dataWithItems: _items count: 1 itemSize: 4096] hash]); +} + +- (void)testCompare +{ + OFData *data1 = [self.dataClass dataWithItems: "aa" count: 2]; + OFData *data2 = [self.dataClass dataWithItems: "ab" count: 2]; + OFData *data3 = [self.dataClass dataWithItems: "aaa" count: 3]; + + OTAssertEqual([data1 compare: data2], OFOrderedAscending); + OTAssertEqual([data2 compare: data1], OFOrderedDescending); + OTAssertEqual([data1 compare: data1], OFOrderedSame); + OTAssertEqual([data1 compare: data3], OFOrderedAscending); + OTAssertEqual([data2 compare: data3], OFOrderedDescending); +} + +- (void)testCopy +{ + OTAssertEqualObjects([[_data copy] autorelease], _data); +} + +- (void)testRangeOfDataOptionsRange +{ + OFData *data = [self.dataClass dataWithItems: "aaabaccdacaabb" + count: 7 + itemSize: 2]; + OFRange range; + + range = [data rangeOfData: [self.dataClass dataWithItems: "aa" + count: 1 + itemSize: 2] + options: 0 + range: OFMakeRange(0, 7)]; + OTAssertEqual(range.location, 0); + OTAssertEqual(range.length, 1); + + range = [data rangeOfData: [self.dataClass dataWithItems: "aa" + count: 1 + itemSize: 2] + options: OFDataSearchBackwards + range: OFMakeRange(0, 7)]; + OTAssertEqual(range.location, 5); + OTAssertEqual(range.length, 1); + + range = [data rangeOfData: [self.dataClass dataWithItems: "ac" + count: 1 + itemSize: 2] + options: 0 + range: OFMakeRange(0, 7)]; + OTAssertEqual(range.location, 2); + OTAssertEqual(range.length, 1); + + range = [data rangeOfData: [self.dataClass dataWithItems: "aabb" + count: 2 + itemSize: 2] + options: 0 + range: OFMakeRange(0, 7)]; + OTAssertEqual(range.location, 5); + OTAssertEqual(range.length, 2); + + range = [data rangeOfData: [self.dataClass dataWithItems: "aa" + count: 1 + itemSize: 2] + options: 0 + range: OFMakeRange(1, 6)]; + OTAssertEqual(range.location, 5); + OTAssertEqual(range.length, 1); + + range = [data rangeOfData: [self.dataClass dataWithItems: "aa" + count: 1 + itemSize: 2] + options: OFDataSearchBackwards + range: OFMakeRange(0, 5)]; + OTAssertEqual(range.location, 0); + OTAssertEqual(range.length, 1); +} + +- (void)testRangeOfDataOptionsRangeThrowsOnDifferentItemSize +{ + OTAssertThrowsSpecific( + [_data rangeOfData: [OFData dataWithItems: "a" count: 1] + options: 0 + range: OFMakeRange(0, 1)], + OFInvalidArgumentException); +} + +- (void)testRangeOfDataOptionsRangeThrowsOnOutOfRangeRange +{ + OTAssertThrowsSpecific( + [_data rangeOfData: [OFData dataWithItemSize: 4096] + options: 0 + range: OFMakeRange(1, 2)], + OFOutOfRangeException); + + OTAssertThrowsSpecific( + [_data rangeOfData: [OFData dataWithItemSize: 4096] + options: 0 + range: OFMakeRange(2, 1)], + OFOutOfRangeException); +} + +- (void)testSubdataWithRange +{ + OFData *data1 = [self.dataClass dataWithItems: "aaabaccdacaabb" + count: 7 + itemSize: 2]; + OFData *data2 = [self.dataClass dataWithItems: "abcde" count: 5]; + + OTAssertEqualObjects( + [data1 subdataWithRange: OFMakeRange(2, 4)], + [OFData dataWithItems: "accdacaa" count: 4 itemSize: 2]); + + OTAssertEqualObjects( + [data2 subdataWithRange: OFMakeRange(2, 3)], + [OFData dataWithItems: "cde" count: 3]); +} + +- (void)testSubdataWithRangeThrowsOnOutOfRangeRange +{ + OFData *data1 = [self.dataClass dataWithItems: "aaabaccdacaabb" + count: 7 + itemSize: 2]; + OFData *data2 = [self.dataClass dataWithItems: "abcde" count: 5]; + + OTAssertThrowsSpecific([data1 subdataWithRange: OFMakeRange(7, 1)], + OFOutOfRangeException); + + OTAssertThrowsSpecific([data1 subdataWithRange: OFMakeRange(8, 0)], + OFOutOfRangeException); + + OTAssertThrowsSpecific([data2 subdataWithRange: OFMakeRange(6, 1)], + OFOutOfRangeException); +} + +- (void)testStringByMD5Hashing +{ + OTAssertEqualObjects(_data.stringByMD5Hashing, + @"37d65c8816008d58175b1d71ee892de3"); +} + +- (void)testStringByRIPEMD160Hashing +{ + OTAssertEqualObjects(_data.stringByRIPEMD160Hashing, + @"ab33a6a725f9fcec6299054dc604c0eb650cd889"); +} + +- (void)testStringBySHA1Hashing +{ + OTAssertEqualObjects(_data.stringBySHA1Hashing, + @"eb50cfcc29d0bed96b3bafe03e99110bcf6663b3"); +} + +- (void)testStringBySHA224Hashing +{ + OTAssertEqualObjects(_data.stringBySHA224Hashing, + @"204f8418a914a6828f8eb27871e01f74366f6d8fac8936029ebf0041"); +} + +- (void)testStringBySHA256Hashing +{ + OTAssertEqualObjects(_data.stringBySHA256Hashing, + @"27c521859f6f5b10aeac4e210a6d005c" + @"85e382c594e2622af9c46c6da8906821"); +} + +- (void)testStringBySHA384Hashing +{ + OTAssertEqualObjects(_data.stringBySHA384Hashing, + @"af99a52c26c00f01fe649dcc53d7c7a0" + @"a9ee0150b971955be2af395708966120" + @"5f2634f70df083ef63b232d5b8549db4"); +} + +- (void)testStringBySHA512Hashing +{ + OTAssertEqualObjects(_data.stringBySHA512Hashing, + @"1cbd53bf8bed9b45a63edda645ee1217" + @"24d2f0323c865e1039ba13320bc6c66e" + @"c79b6cdf6d08395c612b7decb1e59ad1" + @"e72bfa007c2f76a823d10204d47d2e2d"); +} + +- (void)testStringByBase64Encoding +{ + OTAssertEqualObjects([[self.dataClass dataWithItems: "abcde" count: 5] + stringByBase64Encoding], @"YWJjZGU="); +} + +- (void)testDataWithBase64EncodedString +{ + OTAssertEqualObjects( + [self.dataClass dataWithBase64EncodedString: @"YWJjZGU="], + [OFData dataWithItems: "abcde" count: 5]); +} +@end ADDED new_tests/OFMutableDataTests.m Index: new_tests/OFMutableDataTests.m ================================================================== --- new_tests/OFMutableDataTests.m +++ new_tests/OFMutableDataTests.m @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2008-2024 Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#include + +#import "OFDataTests.h" + +@interface OFMutableDataTests: OFDataTests +{ + OFMutableData *_mutableData; +} +@end + +@implementation OFMutableDataTests +- (Class)dataClass +{ + return [OFMutableData class]; +} + +- (void)setUp +{ + [super setUp]; + + _mutableData = [[OFMutableData alloc] initWithItems: "abcdef" count: 6]; +} + +- (void)dealloc +{ + [_mutableData release]; + + [super dealloc]; +} + +- (void)testMutableCopy +{ + OTAssertEqualObjects([[_data mutableCopy] autorelease], _data); + OTAssertNotEqual([[_data mutableCopy] autorelease], _data); +} + +- (void)testAddItem +{ + [_mutableData addItem: "g"]; + + OTAssertEqualObjects(_mutableData, + [OFData dataWithItems: "abcdefg" count: 7]); +} + +- (void)testAddItemsCount +{ + [_mutableData addItems: "gh" count: 2]; + + OTAssertEqualObjects(_mutableData, + [OFData dataWithItems: "abcdefgh" count: 8]); +} + +- (void)testAddItemsCountThrowsOnOutOfRange +{ + OTAssertThrowsSpecific([_mutableData addItems: "" count: SIZE_MAX], + OFOutOfRangeException); +} + +- (void)testRemoveLastItem +{ + [_mutableData removeLastItem]; + + OTAssertEqualObjects(_mutableData, + [OFData dataWithItems: "abcde" count: 5]); +} + +- (void)testRemoveItemsInRange +{ + [_mutableData removeItemsInRange: OFMakeRange(1, 2)]; + + OTAssertEqualObjects(_mutableData, + [OFData dataWithItems: "adef" count: 4]); +} + +- (void)testRemoveItemsInRangeThrowsOnOutOfRangeRange +{ + OTAssertThrowsSpecific( + [_mutableData removeItemsInRange: OFMakeRange(6, 1)], + OFOutOfRangeException); + + OTAssertThrowsSpecific( + [_mutableData removeItemsInRange: OFMakeRange(7, 0)], + OFOutOfRangeException); +} + +- (void)testInsertItemsAtIndexCount +{ + [_mutableData insertItems: "BC" atIndex: 1 count: 2]; + + OTAssertEqualObjects(_mutableData, + [OFData dataWithItems: "aBCbcdef" count: 8]); +} + +- (void)testInsertItemsAtIndexCountThrowsOnOutOfRangeIndex +{ + OTAssertThrowsSpecific( + [_mutableData insertItems: "a" atIndex: 7 count: 1], + OFOutOfRangeException); +} +@end Index: tests/Makefile ================================================================== --- tests/Makefile +++ tests/Makefile @@ -11,12 +11,11 @@ ${PROG_NOINST}.rpx DISTCLEAN = Info.plist PROG_NOINST = tests${PROG_SUFFIX} STATIC_LIB_NOINST = ${TESTS_STATIC_LIB} -SRCS = OFDataTests.m \ - OFMemoryStreamTests.m \ +SRCS = OFMemoryStreamTests.m \ OFStreamTests.m \ OFValueTests.m \ OFXMLNodeTests.m \ OFXMLParserTests.m \ TestsAppDelegate.m DELETED tests/OFDataTests.m Index: tests/OFDataTests.m ================================================================== --- tests/OFDataTests.m +++ tests/OFDataTests.m @@ -1,228 +0,0 @@ -/* - * Copyright (c) 2008-2024 Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#include "config.h" - -#include - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFData"; - -@implementation TestsAppDelegate (OFDataTests) -- (void)dataTests -{ - void *pool = objc_autoreleasePoolPush(); - OFMutableData *mutableData; - OFData *data; - void *raw[2]; - OFRange range; - - TEST(@"+[dataWithItemSize:]", - (mutableData = [OFMutableData dataWithItemSize: 4096])) - - raw[0] = OFAllocMemory(1, 4096); - raw[1] = OFAllocMemory(1, 4096); - memset(raw[0], 0xFF, 4096); - memset(raw[1], 0x42, 4096); - - TEST(@"-[addItem:]", R([mutableData addItem: raw[0]]) && - R([mutableData addItem: raw[1]])) - - TEST(@"-[itemAtIndex:]", - memcmp([mutableData itemAtIndex: 0], raw[0], 4096) == 0 && - memcmp([mutableData itemAtIndex: 1], raw[1], 4096) == 0) - - TEST(@"-[lastItem]", memcmp(mutableData.lastItem, raw[1], 4096) == 0) - - TEST(@"-[count]", mutableData.count == 2) - - TEST(@"-[isEqual:]", - (data = [OFData dataWithItems: mutableData.items - count: mutableData.count - itemSize: mutableData.itemSize]) && - [data isEqual: mutableData] && - R([mutableData removeLastItem]) && ![mutableData isEqual: data]) - - TEST(@"-[mutableCopy]", - (mutableData = [[data mutableCopy] autorelease]) && - [mutableData isEqual: data]) - - TEST(@"-[compare]", [mutableData compare: data] == 0 && - R([mutableData removeLastItem]) && - [data compare: mutableData] == OFOrderedDescending && - [mutableData compare: data] == OFOrderedAscending && - [[OFData dataWithItems: "aa" count: 2] compare: - [OFData dataWithItems: "z" count: 1]] == OFOrderedAscending) - - TEST(@"-[hash]", data.hash == 0x634A529F) - - mutableData = [OFMutableData dataWithItems: "abcdef" count: 6]; - - TEST(@"-[removeLastItem]", - R([mutableData removeLastItem]) && mutableData.count == 5 && - memcmp(mutableData.items, "abcde", 5) == 0) - - TEST(@"-[removeItemsInRange:]", - R([mutableData removeItemsInRange: OFMakeRange(1, 2)]) && - mutableData.count == 3 && memcmp(mutableData.items, "ade", 3) == 0) - - TEST(@"-[insertItems:atIndex:count:]", - R([mutableData insertItems: "bc" atIndex: 1 count: 2]) && - mutableData.count == 5 && - memcmp(mutableData.items, "abcde", 5) == 0) - - data = [OFData dataWithItems: "aaabaccdacaabb" count: 7 itemSize: 2]; - - range = [data rangeOfData: [OFData dataWithItems: "aa" - count: 1 - itemSize: 2] - options: 0 - range: OFMakeRange(0, 7)]; - TEST(@"-[rangeOfData:options:range:] #1", - range.location == 0 && range.length == 1) - - range = [data rangeOfData: [OFData dataWithItems: "aa" - count: 1 - itemSize: 2] - options: OFDataSearchBackwards - range: OFMakeRange(0, 7)]; - TEST(@"-[rangeOfData:options:range:] #2", - range.location == 5 && range.length == 1) - - range = [data rangeOfData: [OFData dataWithItems: "ac" - count: 1 - itemSize: 2] - options: 0 - range: OFMakeRange(0, 7)]; - TEST(@"-[rangeOfData:options:range:] #3", - range.location == 2 && range.length == 1) - - range = [data rangeOfData: [OFData dataWithItems: "aabb" - count: 2 - itemSize: 2] - options: 0 - range: OFMakeRange(0, 7)]; - TEST(@"-[rangeOfData:options:range:] #4", - range.location == 5 && range.length == 2) - - TEST(@"-[rangeOfData:options:range:] #5", - R(range = [data rangeOfData: [OFData dataWithItems: "aa" - count: 1 - itemSize: 2] - options: 0 - range: OFMakeRange(1, 6)]) && - range.location == 5 && range.length == 1) - - range = [data rangeOfData: [OFData dataWithItems: "aa" - count: 1 - itemSize: 2] - options: OFDataSearchBackwards - range: OFMakeRange(0, 5)]; - TEST(@"-[rangeOfData:options:range:] #6", - range.location == 0 && range.length == 1) - - EXPECT_EXCEPTION( - @"-[rangeOfData:options:range:] failing on different itemSize", - OFInvalidArgumentException, - [data rangeOfData: [OFData dataWithItems: "aaa" - count: 1 - itemSize: 3] - options: 0 - range: OFMakeRange(0, 1)]) - - EXPECT_EXCEPTION( - @"-[rangeOfData:options:range:] failing on out of range", - OFOutOfRangeException, - [data rangeOfData: [OFData dataWithItems: "" count: 0 itemSize: 2] - options: 0 - range: OFMakeRange(8, 1)]) - - TEST(@"-[subdataWithRange:]", - [[data subdataWithRange: OFMakeRange(2, 4)] - isEqual: [OFData dataWithItems: "accdacaa" count: 4 itemSize: 2]] && - [[mutableData subdataWithRange: OFMakeRange(2, 3)] - isEqual: [OFData dataWithItems: "cde" count: 3]]) - - EXPECT_EXCEPTION(@"-[subdataWithRange:] failing on out of range #1", - OFOutOfRangeException, - [data subdataWithRange: OFMakeRange(7, 1)]) - - EXPECT_EXCEPTION(@"-[subdataWithRange:] failing on out of range #2", - OFOutOfRangeException, - [mutableData subdataWithRange: OFMakeRange(6, 1)]) - - TEST(@"-[stringByMD5Hashing]", - [mutableData.stringByMD5Hashing - isEqual: @"ab56b4d92b40713acc5af89985d4b786"]) - - TEST(@"-[stringByRIPEMD160Hashing]", - [mutableData.stringByRIPEMD160Hashing - isEqual: @"973398b6e6c6cfa6b5e6a5173f195ce3274bf828"]) - - TEST(@"-[stringBySHA1Hashing]", - [mutableData.stringBySHA1Hashing - isEqual: @"03de6c570bfe24bfc328ccd7ca46b76eadaf4334"]) - - TEST(@"-[stringBySHA224Hashing]", - [mutableData.stringBySHA224Hashing - isEqual: @"bdd03d560993e675516ba5a50638b6531ac2ac3d5847c61916cfced6" - ]) - - TEST(@"-[stringBySHA256Hashing]", - [mutableData.stringBySHA256Hashing - isEqual: @"36bbe50ed96841d10443bcb670d6554f0a34b761be67ec9c4a8ad2c0" - @"c44ca42c"]) - - TEST(@"-[stringBySHA384Hashing]", - [mutableData.stringBySHA384Hashing - isEqual: @"4c525cbeac729eaf4b4665815bc5db0c84fe6300068a727cf74e2813" - @"521565abc0ec57a37ee4d8be89d097c0d2ad52f0"]) - - TEST(@"-[stringBySHA512Hashing]", - [mutableData.stringBySHA512Hashing - isEqual: @"878ae65a92e86cac011a570d4c30a7eaec442b85ce8eca0c2952b5e3" - @"cc0628c2e79d889ad4d5c7c626986d452dd86374b6ffaa7cd8b67665" - @"bef2289a5c70b0a1"]) - - TEST(@"-[stringByBase64Encoding]", - [mutableData.stringByBase64Encoding isEqual: @"YWJjZGU="]) - - TEST(@"+[dataWithBase64EncodedString:]", - memcmp([[OFData dataWithBase64EncodedString: @"YWJjZGU="] items], - "abcde", 5) == 0) - - TEST(@"Building strings", - (mutableData = [OFMutableData dataWithItems: "Hello!" count: 6]) && - R([mutableData addItem: ""]) && - strcmp(mutableData.items, "Hello!") == 0) - - EXPECT_EXCEPTION(@"Detect out of range in -[itemAtIndex:]", - OFOutOfRangeException, [mutableData itemAtIndex: mutableData.count]) - - EXPECT_EXCEPTION(@"Detect out of range in -[addItems:count:]", - OFOutOfRangeException, - [mutableData addItems: raw[0] count: SIZE_MAX]) - - EXPECT_EXCEPTION(@"Detect out of range in -[removeItemsInRange:]", - OFOutOfRangeException, - [mutableData removeItemsInRange: OFMakeRange(mutableData.count, 1)]) - - OFFreeMemory(raw[0]); - OFFreeMemory(raw[1]); - - objc_autoreleasePoolPop(pool); -} -@end