Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -25,11 +25,10 @@ BUILDSYS_LIB AC_DEFINE_UNQUOTED(PLUGIN_SUFFIX, "$PLUGIN_SUFFIX", [Suffix for plugins]) if test x"$PLUGIN_SUFFIX" != "x"; then AC_SUBST(OFPLUGIN_M, "OFPlugin.m") - AC_SUBST(PLUGIN_M, "plugin.m") AC_SUBST(TESTPLUGIN, "plugin") AC_DEFINE(OF_PLUGINS, 1, [Whether we have plugin support]) AC_SUBST(OFPLUGINS_DEF, "-DOF_PLUGINS") fi Index: extra.mk.in ================================================================== --- extra.mk.in +++ extra.mk.in @@ -1,9 +1,8 @@ ASPRINTF_M = @ASPRINTF_M@ OBJC_SYNC = @OBJC_SYNC@ OBJC_SYNC_M = @OBJC_SYNC_M@ OFPLUGIN_M = @OFPLUGIN_M@ -PLUGIN_M = @PLUGIN_M@ TESTPLUGIN = @TESTPLUGIN@ WS2_LIBS = @WS2_LIBS@ TESTS = @TESTS@ TEST_LAUNCHER = @TEST_LAUNCHER@ Index: tests/Makefile ================================================================== --- tests/Makefile +++ tests/Makefile @@ -1,23 +1,23 @@ include ../extra.mk SUBDIRS = ${TESTPLUGIN} PROG_NOINST = tests${PROG_SUFFIX} -SRCS = array.m \ - dataarray.m \ - dictionary.m \ - hashes.m \ - list.m \ - main.m \ - object.m \ - ${PLUGIN_M} \ - string.m \ - tcpsocket.m \ - thread.m \ - xmlelement.m \ - xmlparser.m +SRCS = OFArray.m \ + OFDataArray.m \ + OFDictionary.m \ + OFHashes.m \ + OFList.m \ + OFObject.m \ + ${OFPLUGIN_M} \ + OFString.m \ + OFTCPSocket.m \ + OFThread.m \ + OFXMLElement.m \ + OFXMLParser.m \ + main.m .PHONY: run run-tests run: all if [ x"$$DONT_RUN_TESTS" = x"" ]; then \ ${MAKE} ${MFLAGS} run-tests; \ ADDED tests/OFArray.m Index: tests/OFArray.m ================================================================== --- tests/OFArray.m +++ tests/OFArray.m @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2008 - 2009 + * 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 included in + * the packaging of this file. + */ + +#include "config.h" + +#import "OFArray.h" +#import "OFAutoreleasePool.h" +#import "OFString.h" +#import "OFExceptions.h" + +#import "main.h" + +static OFString *module = @"OFArray"; +static OFString *c_ary[] = { + @"Foo", + @"Bar", + @"Baz", + nil +}; + +void +array_tests() +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFArray *a[3]; + + TEST(@"+[array]", (a[0] = [OFMutableArray array])) + + TEST(@"+[arrayWithObjects:]", + (a[1] = [OFArray arrayWithObjects: @"Foo", @"Bar", @"Baz", nil])) + + TEST(@"+[arrayWithCArray:]", (a[2] = [OFArray arrayWithCArray: c_ary])) + + TEST(@"-[addObject:]", [a[0] addObject: c_ary[0]] && + [a[0] addObject: c_ary[1]] && [a[0] addObject: c_ary[2]]) + + TEST(@"-[count]", [a[0] count] == 3 && [a[1] count] == 3 && + [a[2] count] == 3) + + TEST(@"-[isEqual:]", [a[0] isEqual: a[1]] && [a[1] isEqual: a[2]]) + + TEST(@"-[objectAtIndex:]", + [[a[0] objectAtIndex: 0] isEqual: c_ary[0]] && + [[a[0] objectAtIndex: 1] isEqual: c_ary[1]] && + [[a[0] objectAtIndex: 2] isEqual: c_ary[2]] && + [[a[1] objectAtIndex: 0] isEqual: c_ary[0]] && + [[a[1] objectAtIndex: 1] isEqual: c_ary[1]] && + [[a[1] objectAtIndex: 2] isEqual: c_ary[2]] && + [[a[2] objectAtIndex: 0] isEqual: c_ary[0]] && + [[a[2] objectAtIndex: 1] isEqual: c_ary[1]] && + [[a[2] objectAtIndex: 2] isEqual: c_ary[2]]) + + TEST(@"-[removeNObjects:]", [a[0] removeNObjects: 2] && + [a[0] count] == 1 && [[a[0] objectAtIndex: 0] isEqual: c_ary[0]]) + + EXPECT_EXCEPTION(@"Detect out of range in -[objectAtIndex:]", + OFOutOfRangeException, [a[0] objectAtIndex: [a[0] count]]) + + EXPECT_EXCEPTION(@"Detect out of range in -[removeNItems:]", + OFOutOfRangeException, [a[0] removeNObjects: [a[0] count] + 1]) + + [pool release]; +} ADDED tests/OFDataArray.m Index: tests/OFDataArray.m ================================================================== --- tests/OFDataArray.m +++ tests/OFDataArray.m @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2008 - 2009 + * 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 included in + * the packaging of this file. + */ + +#include "config.h" + +#include + +#import "OFArray.h" +#import "OFAutoreleasePool.h" +#import "OFString.h" +#import "OFExceptions.h" + +#import "main.h" + +static OFString *module; +const char *str = "Hello!"; + +static void +do_tests(Class class) +{ + OFDataArray *array[2]; + void *data[2]; + Class other; + + TEST(@"+[dataArrayWithItemSize:]", + (array[0] = [class dataArrayWithItemSize: 4096])) + + data[0] = [array[0] allocMemoryWithSize: 4096]; + data[1] = [array[0] allocMemoryWithSize: 4096]; + memset(data[0], 0xFF, 4096); + memset(data[1], 0x42, 4096); + + TEST(@"-[addItem:]", [array[0] addItem: data[0]] && + [array[0] addItem: data[1]]) + + TEST(@"-[itemAtIndex:]", + !memcmp([array[0] itemAtIndex: 0], data[0], 4096) && + !memcmp([array[0] itemAtIndex: 1], data[1], 4096)) + + TEST(@"-[lastItem]", !memcmp([array[0] lastItem], data[1], 4096)) + + TEST(@"-[count]", [array[0] count] == 2) + + other = (class == [OFDataArray class] + ? [OFBigDataArray class] + : [OFDataArray class]); + TEST(@"-[isEqual:]", (array[1] = [other dataArrayWithItemSize: 4096]) && + [array[1] addNItems: [array[0] count] + fromCArray: [array[0] cArray]] && + [array[1] isEqual: array[0]] && + [array[1] removeNItems: 1] && ![array[0] isEqual: array[1]]) + + TEST(@"-[copy]", (array[1] = [[array[0] copy] autorelease]) && + [array[0] isEqual: array[1]]) + + TEST(@"-[compare]", [array[0] compare: array[1]] == 0 && + [array[1] removeNItems: 1] && + [array[0] compare: array[1]] == 0x42 && + [array[1] compare: array[0]] == -0x42) + + TEST(@"-[hash]", [array[0] hash] == 0xC54621B6) + + TEST(@"-[removeNItems:]", [array[0] removeNItems: 1]) + + TEST(@"Building strings", + (array[0] = [class dataArrayWithItemSize: 1]) && + [array[0] addNItems: 6 + fromCArray: (void*)str] && [array[0] addItem: ""] && + !strcmp([array[0] cArray], str)) + + EXPECT_EXCEPTION(@"Detect out of range in -[itemAtIndex:]", + OFOutOfRangeException, [array[0] itemAtIndex: [array[0] count]]) + + EXPECT_EXCEPTION(@"Detect out of range in -[addNItems:fromCArray:]", + OFOutOfRangeException, [array[0] addNItems: SIZE_MAX + fromCArray: NULL]) + + EXPECT_EXCEPTION(@"Detect out of range in -[removeNItems:]", + OFOutOfRangeException, + [array[0] removeNItems: [array[0] count] + 1]) +} + +void +dataarray_tests() +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + + module = @"OFDataArray"; + do_tests([OFDataArray class]); + + module = @"OFBigDataArray"; + do_tests([OFBigDataArray class]); + + [pool release]; +} ADDED tests/OFDictionary.m Index: tests/OFDictionary.m ================================================================== --- tests/OFDictionary.m +++ tests/OFDictionary.m @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2008 - 2009 + * 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 included in + * the packaging of this file. + */ + +#include "config.h" + +#import "OFDictionary.h" +#import "OFAutoreleasePool.h" +#import "OFString.h" +#import "OFExceptions.h" + +#import "main.h" + +static OFString *module = @"OFDictionary"; +static OFString *keys[] = { + @"key1", + @"key2" +}; +static OFString *values[] = { + @"value1", + @"value2" +}; + +void +dictionary_tests() +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFDictionary *dict = [OFMutableDictionary dictionary]; + OFIterator *iter; + of_iterator_pair_t pair[3]; + OFArray *akeys, *avalues; + + [dict setObject: values[0] + forKey: keys[0]]; + [dict setObject: values[1] + forKey: keys[1]]; + + TEST(@"-[objectForKey:]", + [[dict objectForKey: keys[0]] isEqual: values[0]] && + [[dict objectForKey: keys[1]] isEqual: values[1]] && + [dict objectForKey: @"key3"] == nil) + + TEST(@"-[iterator]", (iter = [dict iterator])) + + pair[0] = [iter nextKeyObjectPair]; + pair[1] = [iter nextKeyObjectPair]; + pair[2] = [iter nextKeyObjectPair]; + TEST(@"OFIterator's -[nextKeyObjectPair]", + [pair[0].key isEqual: keys[0]] && + [pair[0].object isEqual: values[0]] && + [pair[1].key isEqual: keys[1]] && + [pair[1].object isEqual: values[1]] && + pair[2].key == nil && pair[2].object == nil) + + TEST(@"-[count]", [dict count] == 2) + + TEST(@"+[dictionaryWithKeysAndObjects:]", + (dict = [OFDictionary dictionaryWithKeysAndObjects: @"foo", @"bar", + @"baz", @"qux", + nil]) && + [[dict objectForKey: @"foo"] isEqual: @"bar"] && + [[dict objectForKey: @"baz"] isEqual: @"qux"]) + + TEST(@"+[dictionaryWithObject:forKey:]", + (dict = [OFDictionary dictionaryWithObject: @"bar" + forKey: @"foo"]) && + [[dict objectForKey: @"foo"] isEqual: @"bar"]) + + akeys = [OFArray arrayWithObjects: keys[0], keys[1], nil]; + avalues = [OFArray arrayWithObjects: values[0], values[1], nil]; + TEST(@"+[dictionaryWithObjects:forKeys:]", + (dict = [OFDictionary dictionaryWithObjects: avalues + forKeys: akeys]) && + [[dict objectForKey: keys[0]] isEqual: values[0]] && + [[dict objectForKey: keys[1]] isEqual: values[1]]) + + TEST(@"-[copy]", + (dict = [[dict copy] autorelease]) && + [[dict objectForKey: keys[0]] isEqual: values[0]] && + [[dict objectForKey: keys[1]] isEqual: values[1]]) + + TEST(@"-[mutableCopy]", + (dict = [[dict mutableCopy] autorelease]) && + [[dict objectForKey: keys[0]] isEqual: values[0]] && + [[dict objectForKey: keys[1]] isEqual: values[1]] && + [dict setObject: @"value3" + forKey: @"key3"] && + [[dict objectForKey: @"key3"] isEqual: @"value3"] && + [dict setObject: @"foo" + forKey: keys[0]] && + [[dict objectForKey: keys[0]] isEqual: @"foo"]) + + TEST(@"-[removeObjectForKey:]", + [dict removeObjectForKey: keys[0]] && + [dict objectForKey: keys[0]] == nil) + + [pool release]; +} ADDED tests/OFHashes.m Index: tests/OFHashes.m ================================================================== --- tests/OFHashes.m +++ tests/OFHashes.m @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2008 - 2009 + * 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 included in + * the packaging of this file. + */ + +#include "config.h" + +#include + +#import "OFHashes.h" +#import "OFFile.h" +#import "OFAutoreleasePool.h" +#import "OFString.h" +#import "OFExceptions.h" + +#import "main.h" + +static OFString *module = @"OFHashes"; + +const uint8_t testfile_md5[OF_MD5_DIGEST_SIZE] = + "\x00\x8B\x9D\x1B\x58\xDF\xF8\xFE\xEE\xF3\xAE\x8D\xBB\x68\x2D\x38"; +const uint8_t testfile_sha1[OF_SHA1_DIGEST_SIZE] = + "\xC9\x9A\xB8\x7E\x1E\xC8\xEC\x65\xD5\xEB\xE4\x2E\x0D\xA6\x80\x96\xF5" + "\x94\xE7\x17"; + +void +hashes_tests() +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFMD5Hash *md5; + OFSHA1Hash *sha1; + OFFile *f = [OFFile fileWithPath: @"testfile" + mode: @"rb"]; + + TEST(@"+[md5Hash]", (md5 = [OFMD5Hash md5Hash])) + + TEST(@"+[sha1Hash]", (sha1 = [OFSHA1Hash sha1Hash])) + + while (![f atEndOfStream]) { + char buf[64]; + size_t len = [f readNBytes: 64 + intoBuffer: buf]; + [md5 updateWithBuffer: buf + ofSize: len]; + [sha1 updateWithBuffer: buf + ofSize: len]; + } + [f close]; + + TEST(@"-[digest]", + !memcmp([md5 digest], testfile_md5, OF_MD5_DIGEST_SIZE) && + !memcmp([sha1 digest], testfile_sha1, OF_MD5_DIGEST_SIZE)) + + [pool release]; +} ADDED tests/OFList.m Index: tests/OFList.m ================================================================== --- tests/OFList.m +++ tests/OFList.m @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2008 - 2009 + * 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 included in + * the packaging of this file. + */ + +#include "config.h" + +#import "OFList.h" +#import "OFAutoreleasePool.h" +#import "OFString.h" +#import "OFExceptions.h" + +#import "main.h" + +static OFString *module = @"OFList"; +static OFString *strings[] = { + @"Foo", + @"Bar", + @"Baz" +}; + +void +list_tests() +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFList *list; + + TEST(@"+[list]", (list = [OFList list])) + + TEST(@"-[append:]", [list append: strings[0]] && + [list append: strings[1]] && [list append: strings[2]]) + + TEST(@"-[first]", [[list first]->object isEqual: strings[0]]) + + TEST(@"-[first]->next", + [[list first]->next->object isEqual: strings[1]]) + + TEST(@"-[last]", [[list last]->object isEqual: strings[2]]) + + TEST(@"-[last]->prev", [[list last]->prev->object isEqual: strings[1]]) + + TEST(@"-[remove:]", [list remove: [list last]] && + [[list last]->object isEqual: strings[1]] && + [list remove: [list first]] && + [[list first]->object isEqual: [list last]->object]) + + TEST(@"-[insert:before:]", [list insert: strings[0] + before: [list last]] && + [[list last]->prev->object isEqual: strings[0]]) + + + TEST(@"-[insert:after:]", [list insert: strings[2] + after: [list first]->next] && + [[list last]->object isEqual: strings[2]]) + + TEST(@"-[count]", [list count] == 3) + + TEST(@"-[copy]", (list = [[list copy] autorelease]) && + [[list first]->object isEqual: strings[0]] && + [[list first]->next->object isEqual: strings[1]] && + [[list last]->object isEqual: strings[2]]) + + TEST(@"-[isEqual:]", [list isEqual: [[list copy] autorelease]]) + + [pool release]; +} ADDED tests/OFObject.m Index: tests/OFObject.m ================================================================== --- tests/OFObject.m +++ tests/OFObject.m @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2008 - 2009 + * 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 included in + * the packaging of this file. + */ + +#include "config.h" + +#import "OFAutoreleasePool.h" +#import "OFExceptions.h" + +#import "main.h" + +static OFString *module = @"OFObject"; + +void +object_tests() +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFObject *obj = [[[OFObject alloc] init] autorelease]; + void *p, *q, *r; + + EXPECT_EXCEPTION(@"Detect freeing of memory not allocated by object", + OFMemoryNotPartOfObjectException, [obj freeMemory: NULL]) + + TEST(@"Allocating 4096 bytes", + (p = [obj allocMemoryWithSize: 4096]) != NULL) + + TEST(@"Freeing memory", [obj freeMemory: p]) + + EXPECT_EXCEPTION(@"Detect freeing of memory twice", + OFMemoryNotPartOfObjectException, [obj freeMemory: p]) + + TEST(@"Allocating and freeing 4096 bytes 3 times", + (p = [obj allocMemoryWithSize: 4096]) != NULL && + (q = [obj allocMemoryWithSize: 4096]) != NULL && + (r = [obj allocMemoryWithSize: 4096]) != NULL && + [obj freeMemory: p] && [obj freeMemory: q] && [obj freeMemory: r]) + + EXPECT_EXCEPTION(@"Detect out of memory on alloc", + OFOutOfMemoryException, [obj allocMemoryWithSize: SIZE_MAX]) + + EXPECT_EXCEPTION(@"Detect out of memory on resize", + OFOutOfMemoryException, + { + p = [obj allocMemoryWithSize: 1]; + [obj resizeMemory: p + toSize: SIZE_MAX]; + }) + [obj freeMemory: p]; + + TEST(@"Allocate when trying to resize NULL", + (p = [obj resizeMemory: NULL + toSize: 1024]) != NULL) + [obj freeMemory: p]; + + EXPECT_EXCEPTION(@"Detect resizing of memory not allocated by object", + OFMemoryNotPartOfObjectException, [obj resizeMemory: (void*)1 + toSize: 1024]) + + [pool release]; +} ADDED tests/OFPlugin.m Index: tests/OFPlugin.m ================================================================== --- tests/OFPlugin.m +++ tests/OFPlugin.m @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2008 - 2009 + * 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 included in + * the packaging of this file. + */ + +#include "config.h" + +#import "OFPlugin.h" +#import "OFAutoreleasePool.h" +#import "OFString.h" +#import "OFExceptions.h" + +#import "main.h" + +#import "plugin/TestPlugin.h" + +static OFString *module = @"OFPlugin"; + +void +plugin_tests() +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + TestPlugin *plugin; + + TEST(@"+[pluginFromFile:]", + (plugin = [OFPlugin pluginFromFile: @"plugin/TestPlugin"])) + + TEST(@"TestPlugin's -[test:]", [plugin test: 1234] == 2468) + + [pool release]; +} ADDED tests/OFString.m Index: tests/OFString.m ================================================================== --- tests/OFString.m +++ tests/OFString.m @@ -0,0 +1,316 @@ +/* + * Copyright (c) 2008 - 2009 + * 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 included in + * the packaging of this file. + */ + +#include "config.h" + +#import "OFString.h" +#import "OFArray.h" +#import "OFAutoreleasePool.h" +#import "OFExceptions.h" + +#import "main.h" + +static OFString *module = @"OFString"; +static OFString* whitespace[] = { + @" \r \t\n\t \tasd \t \t\t\r\n", + @" \t\t \t\t \t \t" +}; + +@interface EntityHandler: OFObject +@end + +@implementation EntityHandler +- (OFString*)foundUnknownEntityNamed: (OFString*)entity +{ + if ([entity isEqual: @"foo"]) + return @"bar"; + + return nil; +} +@end + +void +string_tests() +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFString *s[3]; + OFArray *a; + int i; + EntityHandler *h; + + s[0] = [OFMutableString stringWithString: @"täs€"]; + s[1] = [OFMutableString string]; + s[2] = [[s[0] copy] autorelease]; + + TEST(@"-[isEqual:]", [s[0] isEqual: s[2]] && + ![s[0] isEqual: [[[OFObject alloc] init] autorelease]]) + + TEST(@"-[compare:]", [s[0] compare: s[2]] == 0 && + [s[0] compare: @""] != 0) + + TEST(@"-[hash] is the same if -[isEqual:] is YES", + [s[0] hash] == [s[2] hash]) + + TEST(@"-[appendString:] and -[appendCString:]", + [s[1] appendCString: "1𝄞"] && [s[1] appendString: @"3"] && + [[s[0] appendString: s[1]] isEqual: @"täs€1𝄞3"]) + + TEST(@"-[length]", [s[0] length] == 7) + TEST(@"-[cStringLength]", [s[0] cStringLength] == 13) + TEST(@"-[hash]", [s[0] hash] == 0x8AC1EEF6) + + TEST(@"-[characterAtIndex:]", [s[0] characterAtIndex: 0] == 't' && + [s[0] characterAtIndex: 1] == 0xE4 && + [s[0] characterAtIndex: 3] == 0x20AC && + [s[0] characterAtIndex: 5] == 0x1D11E) + + EXPECT_EXCEPTION(@"Detect out of range in -[characterAtIndex:]", + OFOutOfRangeException, [s[0] characterAtIndex: 7]) + + TEST(@"-[reverse]", [[s[0] reverse] isEqual: @"3𝄞1€sät"]) + + s[1] = [OFMutableString stringWithString: @"abc"]; + + TEST(@"-[upper]", [[s[0] upper] isEqual: @"3𝄞1€SÄT"] && + [[s[1] upper] isEqual: @"ABC"]) + + TEST(@"-[lower]", [[s[0] lower] isEqual: @"3𝄞1€sät"] && + [[s[1] lower] isEqual: @"abc"]) + + TEST(@"+[stringWithCString:length:]", + (s[0] = [OFMutableString stringWithCString: "foobar" + length: 3]) && + [s[0] isEqual: @"foo"]) + + TEST(@"-[appendCStringWithLength:]", + [[s[0] appendCString: "foobarqux" + 3 + withLength: 3] isEqual: @"foobar"]) + + EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #1", + OFInvalidEncodingException, + [OFString stringWithCString: "\xE0\x80"]) + EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #2", + OFInvalidEncodingException, + [OFString stringWithCString: "\xF0\x80\x80\xC0"]) + + TEST(@"-[reverse] on UTF-8 strings", + (s[0] = [[OFMutableString stringWithCString: "äöü€𝄞"] reverse]) && + [s[0] isEqual: @"𝄞€üöä"]) + + TEST(@"Conversion of ISO 8859-1 to UTF-8", + [[OFString stringWithCString: "\xE4\xF6\xFC" + encoding: OF_STRING_ENCODING_ISO_8859_1] + isEqual: @"äöü"]) + + TEST(@"Conversion of ISO 8859-15 to UTF-8", + [[OFString stringWithCString: "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE" + encoding: OF_STRING_ENCODING_ISO_8859_15] + isEqual: @"€ŠšŽžŒœŸ"]) + + TEST(@"Conversion of Windows 1252 to UTF-8", + [[OFString stringWithCString: "\x80\x82\x83\x84\x85\x86\x87\x88" + "\x89\x8A\x8B\x8C\x8E\x91\x92\x93" + "\x94\x95\x96\x97\x98\x99\x9A\x9B" + "\x9C\x9E\x9F" + encoding: OF_STRING_ENCODING_WINDOWS_1252] + isEqual: @"€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ"]) + + TEST(@"+[stringWithFormat:]", + [(s[0] = [OFMutableString stringWithFormat: @"%s: %d", "test", 123]) + isEqual: @"test: 123"]) + + TEST(@"-[appendWithFormat:]", + [([s[0] appendWithFormat: @"%02X", 15]) isEqual: @"test: 1230F"]) + + TEST(@"-[indexOfFirstOccurrenceOfString:]", + [@"𝄞öö" indexOfFirstOccurrenceOfString: @"öö"] == 1 && + [@"𝄞öö" indexOfFirstOccurrenceOfString: @"ö"] == 1 && + [@"𝄞öö" indexOfFirstOccurrenceOfString: @"𝄞"] == 0 && + [@"𝄞öö" indexOfFirstOccurrenceOfString: @"x"] == SIZE_MAX) + + TEST(@"-[indexOfLastOccurrenceOfString:]", + [@"𝄞öö" indexOfLastOccurrenceOfString: @"öö"] == 1 && + [@"𝄞öö" indexOfLastOccurrenceOfString: @"ö"] == 2 && + [@"𝄞öö" indexOfLastOccurrenceOfString: @"𝄞"] == 0 && + [@"𝄞öö" indexOfLastOccurrenceOfString: @"x"] == SIZE_MAX) + + TEST(@"-[substringFromIndexToIndex:]", + [[@"𝄞öö" substringFromIndex: 1 + toIndex: 2] isEqual: @"ö"] && + [[@"𝄞öö" substringFromIndex: 3 + toIndex: 3] isEqual: @""]) + + EXPECT_EXCEPTION(@"Detect out of range in " + @"-[substringFromIndex:toIndex:] #1", OFOutOfRangeException, + [@"𝄞öö" substringFromIndex: 2 + toIndex: 4]) + EXPECT_EXCEPTION(@"Detect out of range in " + @"-[substringFromIndex:toIndex:] #2", OFOutOfRangeException, + [@"𝄞öö" substringFromIndex: 4 + toIndex: 4]) + + EXPECT_EXCEPTION(@"Detect start > end in " + @"-[substringFromIndex:toIndex:]", OFInvalidArgumentException, + [@"𝄞öö" substringFromIndex: 2 + toIndex: 0]) + + TEST(@"-[stringByAppendingString:]", + [[@"foo" stringByAppendingString: @"bar"] isEqual: @"foobar"]) + + TEST(@"-[hasPrefix:]", [@"foobar" hasPrefix: @"foo"] && + ![@"foobar" hasPrefix: @"foobar0"]) + + TEST(@"-[hasSuffix:]", [@"foobar" hasSuffix: @"bar"] && + ![@"foobar" hasSuffix: @"foobar0"]) + + i = 0; + TEST(@"-[splitWithDelimiter:]", + (a = [@"fooXXbarXXXXbazXXXX" splitWithDelimiter: @"XX"]) && + [[a objectAtIndex: i++] isEqual: @"foo"] && + [[a objectAtIndex: i++] isEqual: @"bar"] && + [[a objectAtIndex: i++] isEqual: @""] && + [[a objectAtIndex: i++] isEqual: @"baz"] && + [[a objectAtIndex: i++] isEqual: @""] && + [[a objectAtIndex: i++] isEqual: @""]) + + TEST(@"-[decimalValueAsInteger]", + [@"1234" decimalValueAsInteger] == 1234 && + [@"" decimalValueAsInteger] == 0) + + TEST(@"-[hexadecimalValueAsInteger]", + [@"123f" hexadecimalValueAsInteger] == 0x123f && + [@"0xABcd" hexadecimalValueAsInteger] == 0xABCD && + [@"xbCDE" hexadecimalValueAsInteger] == 0xBCDE && + [@"$CdEf" hexadecimalValueAsInteger] == 0xCDEF && + [@"" hexadecimalValueAsInteger] == 0) + + EXPECT_EXCEPTION(@"Detect invalid characters in " + @"-[decimalValueAsInteger] #1", OFInvalidEncodingException, + [@"abc" decimalValueAsInteger]) + EXPECT_EXCEPTION(@"Detect invalid characters in " + @"-[decimalValueAsInteger] #2", OFInvalidEncodingException, + [@"0a" decimalValueAsInteger]) + + EXPECT_EXCEPTION(@"Detect invalid chars in " + @"-[hexadecimalValueAsInteger] #1", OFInvalidEncodingException, + [@"0xABCDEFG" hexadecimalValueAsInteger]) + EXPECT_EXCEPTION(@"Detect invalid chars in " + @"-[hexadecimalValueAsInteger] #2", OFInvalidEncodingException, + [@"0x" hexadecimalValueAsInteger]) + EXPECT_EXCEPTION(@"Detect invalid chars in " + @"-[hexadecimalValueAsInteger] #3", OFInvalidEncodingException, + [@"$" hexadecimalValueAsInteger]) + + TEST(@"-[md5Hash]", [[@"asdfoobar" md5Hash] + isEqual: @"184dce2ec49b5422c7cfd8728864db4c"]) + + TEST(@"-[sha1Hash]", [[@"asdfoobar" sha1Hash] + isEqual: @"f5f81ac0a8b5cbfdc4585ec1ad32e7b3a12b9b49"]) + + TEST(@"-[stringByURLEncoding]", + [[@"foo\"ba'_~$" stringByURLEncoding] isEqual: @"foo%22ba%27_~%24"]) + + TEST(@"-[stringByURLDecoding]", + [[@"foo%20bar%22+%24" stringByURLDecoding] isEqual: @"foo bar\" $"]) + + EXPECT_EXCEPTION(@"Detect invalid encoding in -[stringByURLDecoding] " + @"#1", OFInvalidEncodingException, [@"foo%bar" stringByURLDecoding]) + EXPECT_EXCEPTION(@"Detect invalid encoding in -[stringByURLDecoding] " + @"#2", OFInvalidEncodingException, + [@"foo%FFbar" stringByURLDecoding]) + + TEST(@"-[removeCharactersFromIndex:toIndex:]", + (s[0] = [OFMutableString stringWithString: @"𝄞öööbä€"]) && + [s[0] removeCharactersFromIndex: 1 + toIndex: 4] && + [s[0] isEqual: @"𝄞bä€"] && + [s[0] removeCharactersFromIndex: 0 + toIndex: 4] && + [s[0] isEqual: @""]) + + EXPECT_EXCEPTION(@"Detect OoR in " + @"-[removeCharactersFromIndex:toIndex:] #1", OFOutOfRangeException, + { + s[0] = [OFMutableString stringWithString: @"𝄞öö"]; + [s[0] substringFromIndex: 2 + toIndex: 4]; + }) + + EXPECT_EXCEPTION(@"Detect OoR in " + @"-[removeCharactersFromIndex:toIndex:] #2", OFOutOfRangeException, + [s[0] substringFromIndex: 4 + toIndex: 4]) + + EXPECT_EXCEPTION(@"Detect s > e in " + @"-[removeCharactersFromIndex:toIndex:]", + OFInvalidArgumentException, + [s[0] substringFromIndex: 2 + toIndex: 0]) + + TEST(@"-[replaceOccurrencesOfString:withString:]", + [[[OFMutableString stringWithString: @"asd fo asd fofo asd"] + replaceOccurrencesOfString: @"fo" + withString: @"foo"] + isEqual: @"asd foo asd foofoo asd"] && + [[[OFMutableString stringWithString: @"XX"] + replaceOccurrencesOfString: @"X" + withString: @"XX"] + isEqual: @"XXXX"]) + + TEST(@"-[removeLeadingWhitespaces]", + (s[0] = [OFMutableString stringWithString: whitespace[0]]) && + [[s[0] removeLeadingWhitespaces] isEqual: @"asd \t \t\t\r\n"] && + (s[0] = [OFMutableString stringWithString: whitespace[1]]) && + [[s[0] removeLeadingWhitespaces] isEqual: @""]) + + TEST(@"-[removeTrailingWhitespaces]", + (s[0] = [OFMutableString stringWithString: whitespace[0]]) && + [[s[0] removeTrailingWhitespaces] isEqual: @" \r \t\n\t \tasd"] && + (s[0] = [OFMutableString stringWithString: whitespace[1]]) && + [[s[0] removeTrailingWhitespaces] isEqual: @""]) + + TEST(@"-[removeLeadingAndTrailingWhitespaces]", + (s[0] = [OFMutableString stringWithString: whitespace[0]]) && + [[s[0] removeLeadingAndTrailingWhitespaces] isEqual: @"asd"] && + (s[0] = [OFMutableString stringWithString: whitespace[1]]) && + [[s[0] removeLeadingAndTrailingWhitespaces] isEqual: @""]) + + TEST(@"-[stringByXMLEscaping]", + (s[0] = [@" &world'\"!&" stringByXMLEscaping]) && + [s[0] isEqual: @"<hello> &world'"!&"]) + + TEST(@"-[stringByXMLUnescaping]", + [[s[0] stringByXMLUnescaping] isEqual: @" &world'\"!&"] && + [[@"y" stringByXMLUnescaping] isEqual: @"y"] && + [[@"ä" stringByXMLUnescaping] isEqual: @"ä"] && + [[@"€" stringByXMLUnescaping] isEqual: @"€"] && + [[@"𝄞" stringByXMLUnescaping] isEqual: @"𝄞"]) + + EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " + @"#1", OFInvalidEncodingException, [@"&foo;" stringByXMLUnescaping]) + EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " + @"#2", OFInvalidEncodingException, [@"x&" stringByXMLUnescaping]) + EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " + @"#3", OFInvalidEncodingException, [@"&#;" stringByXMLUnescaping]) + EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " + @"#4", OFInvalidEncodingException, [@"&#x;" stringByXMLUnescaping]) + EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " + @"#5", OFInvalidEncodingException, [@"&#g;" stringByXMLUnescaping]) + EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " + @"#6", OFInvalidEncodingException, [@"&#xg;" stringByXMLUnescaping]) + + TEST(@"-[stringByXMLUnescapingWithHandler:]", + (h = [[[EntityHandler alloc] init] autorelease]) && + (s[0] = [@"x&foo;y" stringByXMLUnescapingWithHandler: h]) && + [s[0] isEqual: @"xbary"]) + + [pool release]; +} ADDED tests/OFTCPSocket.m Index: tests/OFTCPSocket.m ================================================================== --- tests/OFTCPSocket.m +++ tests/OFTCPSocket.m @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2008 - 2009 + * 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 included in + * the packaging of this file. + */ + +#include "config.h" + +#include +#include +#include + +#import "OFTCPSocket.h" +#import "OFAutoreleasePool.h" +#import "OFString.h" +#import "OFExceptions.h" +#import "OFMacros.h" + +#import "main.h" + +static OFString *module = @"OFTCPSocket"; + +void +tcpsocket_tests() +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFSocket *server, *client = nil, *accepted; + OFString *service, *msg; + uint16_t port; + char buf[6]; + + srand(time(NULL)); + port = (uint16_t)rand(); + if (port < 1024) + port += 1024; + service = [OFString stringWithFormat: @"%d", port]; + + TEST(@"+[socket]", (server = [OFTCPSocket socket]) && + (client = [OFTCPSocket socket])) + + msg = [OFString stringWithFormat: + @"-[bindService:onNode:withFamily:] (port %d)", port]; + TEST(msg, [server bindService: service + onNode: @"localhost" + withFamily: AF_INET]) + + TEST(@"-[listen]", [server listen]) + + TEST(@"-[connectToService:onNode:]", + [client connectToService: service + onNode: @"localhost"]) + + TEST(@"-[accept]", (accepted = [server accept])) + + TEST(@"-[writeString:]", [client writeString: @"Hello!"]) + + TEST(@"-[readNBytes:intoBuffer:]", [accepted readNBytes: 6 + intoBuffer: buf] && + !memcmp(buf, "Hello!", 6)) + + [pool release]; +} ADDED tests/OFThread.m Index: tests/OFThread.m ================================================================== --- tests/OFThread.m +++ tests/OFThread.m @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2008 - 2009 + * 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 included in + * the packaging of this file. + */ + +#include "config.h" + +#import "OFThread.h" +#import "OFAutoreleasePool.h" +#import "OFString.h" +#import "OFExceptions.h" + +#import "main.h" + +static OFString *module = @"OFThread"; + +@interface TestThread: OFThread +@end + +@implementation TestThread +- main +{ + if ([object isEqual: @"foo"]) + return @"success"; + + return nil; +} +@end + +void +thread_tests() +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + TestThread *t; + OFTLSKey *key; + + TEST(@"+[threadWithObject:]", + (t = [TestThread threadWithObject: @"foo"])) + + TEST(@"-[join]", [[t join] isEqual: @"success"]) + + TEST(@"OFTLSKey's +[tlsKey]", (key = [OFTLSKey tlsKey])) + + TEST(@"+[setObject:forTLSKey:]", [OFThread setObject: @"foo" + forTLSKey: key]) + + TEST(@"+[objectForTLSKey:]", + [[OFThread objectForTLSKey: key] isEqual: @"foo"]) + + [pool release]; +} ADDED tests/OFXMLElement.m Index: tests/OFXMLElement.m ================================================================== --- tests/OFXMLElement.m +++ tests/OFXMLElement.m @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2008 - 2009 + * 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 included in + * the packaging of this file. + */ + +#include "config.h" + +#import "OFXMLElement.h" +#import "OFAutoreleasePool.h" +#import "OFString.h" +#import "OFExceptions.h" + +#import "main.h" + +static OFString *module = @"OFXMLElement"; + +void +xmlelement_tests() +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFXMLElement *elem[2]; + + TEST(@"+[elementWithName:]", + (elem[0] = [OFXMLElement elementWithName: @"foo"]) && + [[elem[0] string] isEqual: @""]) + + TEST(@"+[elementWithName:stringValue:]", + (elem[1] = [OFXMLElement elementWithName: @"foo" + stringValue: @"b&ar"]) && + [[elem[1] string] isEqual: @"b&ar"]) + + TEST(@"-[addAttributeWithName:stringValue:]", + [elem[0] addAttributeWithName: @"foo" + stringValue: @"b&ar"] && + [[elem[0] string] isEqual: @""] && + [elem[1] addAttributeWithName: @"foo" + stringValue: @"b&ar"] && + [[elem[1] string] isEqual: @"b&ar"]) + + TEST(@"-[addChild:]", + [elem[0] addChild: [OFXMLElement elementWithName: @"bar"]] && + [[elem[0] string] isEqual: @""]) + + [pool release]; +} ADDED tests/OFXMLParser.m Index: tests/OFXMLParser.m ================================================================== --- tests/OFXMLParser.m +++ tests/OFXMLParser.m @@ -0,0 +1,190 @@ +/* + * Copyright (c) 2008 - 2009 + * 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 included in + * the packaging of this file. + */ + +#include "config.h" + +#include + +#import "OFXMLParser.h" +#import "OFAutoreleasePool.h" +#import "OFString.h" +#import "OFExceptions.h" + +#import "main.h" + +static OFString *module = @"OFXMLParser"; +static int i = 0; + +enum event_type { + TAG_START, + TAG_END, + STRING, + COMMENT +}; + +static void +callback(enum event_type et, OFString *name, OFString *prefix, OFString *ns, + OFArray *attrs, OFString *string, OFString *comment) +{ + OFString *msg; + id *carray; + size_t count; + + i++; + msg = [OFString stringWithFormat: @"Parsing part #%d", i]; + + switch (i) { + case 1: + case 5: + TEST(msg, et == STRING && [string isEqual: @"bar"]) + break; + case 2: + /* FIXME: Namespace */ + carray = [attrs cArray]; + count = [attrs count]; + + TEST(msg, et == TAG_START && [name isEqual: @"bar"] && + [prefix isEqual: @"foo"] && ns == nil && + attrs != nil && count == 2 && + /* Attribute 1 */ + [[carray[0] name] isEqual: @"bar"] && + [carray[0] prefix] == nil && + [[carray[0] stringValue] isEqual: @"b&az"] && + [carray[0] namespace] == nil && + /* Attribute 2 */ + [[carray[1] name] isEqual: @"qux"] && + [[carray[1] prefix] isEqual: @"qux"] && + [[carray[1] stringValue] isEqual: @" quux "] && + [carray[1] namespace] == nil) + break; + case 3: + TEST(msg, et == STRING && [string isEqual: @"foo\r\n" + "foo<barbar quxbar\r\n" + ""; + size_t j, len; + + TEST(@"+[xmlParser]", (parser = [OFXMLParser xmlParser])) + + TEST(@"-[setDelegate:]", + [parser setDelegate: [[[ParserDelegate alloc] init] autorelease]]) + + /* Simulate a stream where we only get chunks */ + len = strlen(str); + + for (j = 0; j < len; j+= 2) { + if (j + 2 > len) + [parser parseBuffer: str + j + withSize: 1]; + else + [parser parseBuffer: str + j + withSize: 2]; + } + + TEST(@"Checking if everything was parsed", i == 11) + + [pool release]; +} DELETED tests/array.m Index: tests/array.m ================================================================== --- tests/array.m +++ tests/array.m @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * 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 included in - * the packaging of this file. - */ - -#include "config.h" - -#import "OFArray.h" -#import "OFAutoreleasePool.h" -#import "OFString.h" -#import "OFExceptions.h" - -#import "main.h" - -static OFString *module = @"OFArray"; -static OFString *c_ary[] = { - @"Foo", - @"Bar", - @"Baz", - nil -}; - -void -array_tests() -{ - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - OFArray *a[3]; - - TEST(@"+[array]", (a[0] = [OFMutableArray array])) - - TEST(@"+[arrayWithObjects:]", - (a[1] = [OFArray arrayWithObjects: @"Foo", @"Bar", @"Baz", nil])) - - TEST(@"+[arrayWithCArray:]", (a[2] = [OFArray arrayWithCArray: c_ary])) - - TEST(@"-[addObject:]", [a[0] addObject: c_ary[0]] && - [a[0] addObject: c_ary[1]] && [a[0] addObject: c_ary[2]]) - - TEST(@"-[count]", [a[0] count] == 3 && [a[1] count] == 3 && - [a[2] count] == 3) - - TEST(@"-[isEqual:]", [a[0] isEqual: a[1]] && [a[1] isEqual: a[2]]) - - TEST(@"-[objectAtIndex:]", - [[a[0] objectAtIndex: 0] isEqual: c_ary[0]] && - [[a[0] objectAtIndex: 1] isEqual: c_ary[1]] && - [[a[0] objectAtIndex: 2] isEqual: c_ary[2]] && - [[a[1] objectAtIndex: 0] isEqual: c_ary[0]] && - [[a[1] objectAtIndex: 1] isEqual: c_ary[1]] && - [[a[1] objectAtIndex: 2] isEqual: c_ary[2]] && - [[a[2] objectAtIndex: 0] isEqual: c_ary[0]] && - [[a[2] objectAtIndex: 1] isEqual: c_ary[1]] && - [[a[2] objectAtIndex: 2] isEqual: c_ary[2]]) - - TEST(@"-[removeNObjects:]", [a[0] removeNObjects: 2] && - [a[0] count] == 1 && [[a[0] objectAtIndex: 0] isEqual: c_ary[0]]) - - EXPECT_EXCEPTION(@"Detect out of range in -[objectAtIndex:]", - OFOutOfRangeException, [a[0] objectAtIndex: [a[0] count]]) - - EXPECT_EXCEPTION(@"Detect out of range in -[removeNItems:]", - OFOutOfRangeException, [a[0] removeNObjects: [a[0] count] + 1]) - - [pool release]; -} DELETED tests/dataarray.m Index: tests/dataarray.m ================================================================== --- tests/dataarray.m +++ tests/dataarray.m @@ -1,103 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * 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 included in - * the packaging of this file. - */ - -#include "config.h" - -#include - -#import "OFArray.h" -#import "OFAutoreleasePool.h" -#import "OFString.h" -#import "OFExceptions.h" - -#import "main.h" - -static OFString *module; -const char *str = "Hello!"; - -static void -do_tests(Class class) -{ - OFDataArray *array[2]; - void *data[2]; - Class other; - - TEST(@"+[dataArrayWithItemSize:]", - (array[0] = [class dataArrayWithItemSize: 4096])) - - data[0] = [array[0] allocMemoryWithSize: 4096]; - data[1] = [array[0] allocMemoryWithSize: 4096]; - memset(data[0], 0xFF, 4096); - memset(data[1], 0x42, 4096); - - TEST(@"-[addItem:]", [array[0] addItem: data[0]] && - [array[0] addItem: data[1]]) - - TEST(@"-[itemAtIndex:]", - !memcmp([array[0] itemAtIndex: 0], data[0], 4096) && - !memcmp([array[0] itemAtIndex: 1], data[1], 4096)) - - TEST(@"-[lastItem]", !memcmp([array[0] lastItem], data[1], 4096)) - - TEST(@"-[count]", [array[0] count] == 2) - - other = (class == [OFDataArray class] - ? [OFBigDataArray class] - : [OFDataArray class]); - TEST(@"-[isEqual:]", (array[1] = [other dataArrayWithItemSize: 4096]) && - [array[1] addNItems: [array[0] count] - fromCArray: [array[0] cArray]] && - [array[1] isEqual: array[0]] && - [array[1] removeNItems: 1] && ![array[0] isEqual: array[1]]) - - TEST(@"-[copy]", (array[1] = [[array[0] copy] autorelease]) && - [array[0] isEqual: array[1]]) - - TEST(@"-[compare]", [array[0] compare: array[1]] == 0 && - [array[1] removeNItems: 1] && - [array[0] compare: array[1]] == 0x42 && - [array[1] compare: array[0]] == -0x42) - - TEST(@"-[hash]", [array[0] hash] == 0xC54621B6) - - TEST(@"-[removeNItems:]", [array[0] removeNItems: 1]) - - TEST(@"Building strings", - (array[0] = [class dataArrayWithItemSize: 1]) && - [array[0] addNItems: 6 - fromCArray: (void*)str] && [array[0] addItem: ""] && - !strcmp([array[0] cArray], str)) - - EXPECT_EXCEPTION(@"Detect out of range in -[itemAtIndex:]", - OFOutOfRangeException, [array[0] itemAtIndex: [array[0] count]]) - - EXPECT_EXCEPTION(@"Detect out of range in -[addNItems:fromCArray:]", - OFOutOfRangeException, [array[0] addNItems: SIZE_MAX - fromCArray: NULL]) - - EXPECT_EXCEPTION(@"Detect out of range in -[removeNItems:]", - OFOutOfRangeException, - [array[0] removeNItems: [array[0] count] + 1]) -} - -void -dataarray_tests() -{ - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - - module = @"OFDataArray"; - do_tests([OFDataArray class]); - - module = @"OFBigDataArray"; - do_tests([OFBigDataArray class]); - - [pool release]; -} DELETED tests/dictionary.m Index: tests/dictionary.m ================================================================== --- tests/dictionary.m +++ tests/dictionary.m @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * 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 included in - * the packaging of this file. - */ - -#include "config.h" - -#import "OFDictionary.h" -#import "OFAutoreleasePool.h" -#import "OFString.h" -#import "OFExceptions.h" - -#import "main.h" - -static OFString *module = @"OFDictionary"; -static OFString *keys[] = { - @"key1", - @"key2" -}; -static OFString *values[] = { - @"value1", - @"value2" -}; - -void -dictionary_tests() -{ - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - OFDictionary *dict = [OFMutableDictionary dictionary]; - OFIterator *iter; - of_iterator_pair_t pair[3]; - OFArray *akeys, *avalues; - - [dict setObject: values[0] - forKey: keys[0]]; - [dict setObject: values[1] - forKey: keys[1]]; - - TEST(@"-[objectForKey:]", - [[dict objectForKey: keys[0]] isEqual: values[0]] && - [[dict objectForKey: keys[1]] isEqual: values[1]] && - [dict objectForKey: @"key3"] == nil) - - TEST(@"-[iterator]", (iter = [dict iterator])) - - pair[0] = [iter nextKeyObjectPair]; - pair[1] = [iter nextKeyObjectPair]; - pair[2] = [iter nextKeyObjectPair]; - TEST(@"OFIterator's -[nextKeyObjectPair]", - [pair[0].key isEqual: keys[0]] && - [pair[0].object isEqual: values[0]] && - [pair[1].key isEqual: keys[1]] && - [pair[1].object isEqual: values[1]] && - pair[2].key == nil && pair[2].object == nil) - - TEST(@"-[count]", [dict count] == 2) - - TEST(@"+[dictionaryWithKeysAndObjects:]", - (dict = [OFDictionary dictionaryWithKeysAndObjects: @"foo", @"bar", - @"baz", @"qux", - nil]) && - [[dict objectForKey: @"foo"] isEqual: @"bar"] && - [[dict objectForKey: @"baz"] isEqual: @"qux"]) - - TEST(@"+[dictionaryWithObject:forKey:]", - (dict = [OFDictionary dictionaryWithObject: @"bar" - forKey: @"foo"]) && - [[dict objectForKey: @"foo"] isEqual: @"bar"]) - - akeys = [OFArray arrayWithObjects: keys[0], keys[1], nil]; - avalues = [OFArray arrayWithObjects: values[0], values[1], nil]; - TEST(@"+[dictionaryWithObjects:forKeys:]", - (dict = [OFDictionary dictionaryWithObjects: avalues - forKeys: akeys]) && - [[dict objectForKey: keys[0]] isEqual: values[0]] && - [[dict objectForKey: keys[1]] isEqual: values[1]]) - - TEST(@"-[copy]", - (dict = [[dict copy] autorelease]) && - [[dict objectForKey: keys[0]] isEqual: values[0]] && - [[dict objectForKey: keys[1]] isEqual: values[1]]) - - TEST(@"-[mutableCopy]", - (dict = [[dict mutableCopy] autorelease]) && - [[dict objectForKey: keys[0]] isEqual: values[0]] && - [[dict objectForKey: keys[1]] isEqual: values[1]] && - [dict setObject: @"value3" - forKey: @"key3"] && - [[dict objectForKey: @"key3"] isEqual: @"value3"] && - [dict setObject: @"foo" - forKey: keys[0]] && - [[dict objectForKey: keys[0]] isEqual: @"foo"]) - - TEST(@"-[removeObjectForKey:]", - [dict removeObjectForKey: keys[0]] && - [dict objectForKey: keys[0]] == nil) - - [pool release]; -} DELETED tests/hashes.m Index: tests/hashes.m ================================================================== --- tests/hashes.m +++ tests/hashes.m @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * 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 included in - * the packaging of this file. - */ - -#include "config.h" - -#include - -#import "OFHashes.h" -#import "OFFile.h" -#import "OFAutoreleasePool.h" -#import "OFString.h" -#import "OFExceptions.h" - -#import "main.h" - -static OFString *module = @"OFHashes"; - -const uint8_t testfile_md5[OF_MD5_DIGEST_SIZE] = - "\x00\x8B\x9D\x1B\x58\xDF\xF8\xFE\xEE\xF3\xAE\x8D\xBB\x68\x2D\x38"; -const uint8_t testfile_sha1[OF_SHA1_DIGEST_SIZE] = - "\xC9\x9A\xB8\x7E\x1E\xC8\xEC\x65\xD5\xEB\xE4\x2E\x0D\xA6\x80\x96\xF5" - "\x94\xE7\x17"; - -void -hashes_tests() -{ - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - OFMD5Hash *md5; - OFSHA1Hash *sha1; - OFFile *f = [OFFile fileWithPath: @"testfile" - mode: @"rb"]; - - TEST(@"+[md5Hash]", (md5 = [OFMD5Hash md5Hash])) - - TEST(@"+[sha1Hash]", (sha1 = [OFSHA1Hash sha1Hash])) - - while (![f atEndOfStream]) { - char buf[64]; - size_t len = [f readNBytes: 64 - intoBuffer: buf]; - [md5 updateWithBuffer: buf - ofSize: len]; - [sha1 updateWithBuffer: buf - ofSize: len]; - } - [f close]; - - TEST(@"-[digest]", - !memcmp([md5 digest], testfile_md5, OF_MD5_DIGEST_SIZE) && - !memcmp([sha1 digest], testfile_sha1, OF_MD5_DIGEST_SIZE)) - - [pool release]; -} DELETED tests/list.m Index: tests/list.m ================================================================== --- tests/list.m +++ tests/list.m @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * 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 included in - * the packaging of this file. - */ - -#include "config.h" - -#import "OFList.h" -#import "OFAutoreleasePool.h" -#import "OFString.h" -#import "OFExceptions.h" - -#import "main.h" - -static OFString *module = @"OFList"; -static OFString *strings[] = { - @"Foo", - @"Bar", - @"Baz" -}; - -void -list_tests() -{ - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - OFList *list; - - TEST(@"+[list]", (list = [OFList list])) - - TEST(@"-[append:]", [list append: strings[0]] && - [list append: strings[1]] && [list append: strings[2]]) - - TEST(@"-[first]", [[list first]->object isEqual: strings[0]]) - - TEST(@"-[first]->next", - [[list first]->next->object isEqual: strings[1]]) - - TEST(@"-[last]", [[list last]->object isEqual: strings[2]]) - - TEST(@"-[last]->prev", [[list last]->prev->object isEqual: strings[1]]) - - TEST(@"-[remove:]", [list remove: [list last]] && - [[list last]->object isEqual: strings[1]] && - [list remove: [list first]] && - [[list first]->object isEqual: [list last]->object]) - - TEST(@"-[insert:before:]", [list insert: strings[0] - before: [list last]] && - [[list last]->prev->object isEqual: strings[0]]) - - - TEST(@"-[insert:after:]", [list insert: strings[2] - after: [list first]->next] && - [[list last]->object isEqual: strings[2]]) - - TEST(@"-[count]", [list count] == 3) - - TEST(@"-[copy]", (list = [[list copy] autorelease]) && - [[list first]->object isEqual: strings[0]] && - [[list first]->next->object isEqual: strings[1]] && - [[list last]->object isEqual: strings[2]]) - - TEST(@"-[isEqual:]", [list isEqual: [[list copy] autorelease]]) - - [pool release]; -} DELETED tests/object.m Index: tests/object.m ================================================================== --- tests/object.m +++ tests/object.m @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * 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 included in - * the packaging of this file. - */ - -#include "config.h" - -#import "OFAutoreleasePool.h" -#import "OFExceptions.h" - -#import "main.h" - -static OFString *module = @"OFObject"; - -void -object_tests() -{ - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - OFObject *obj = [[[OFObject alloc] init] autorelease]; - void *p, *q, *r; - - EXPECT_EXCEPTION(@"Detect freeing of memory not allocated by object", - OFMemoryNotPartOfObjectException, [obj freeMemory: NULL]) - - TEST(@"Allocating 4096 bytes", - (p = [obj allocMemoryWithSize: 4096]) != NULL) - - TEST(@"Freeing memory", [obj freeMemory: p]) - - EXPECT_EXCEPTION(@"Detect freeing of memory twice", - OFMemoryNotPartOfObjectException, [obj freeMemory: p]) - - TEST(@"Allocating and freeing 4096 bytes 3 times", - (p = [obj allocMemoryWithSize: 4096]) != NULL && - (q = [obj allocMemoryWithSize: 4096]) != NULL && - (r = [obj allocMemoryWithSize: 4096]) != NULL && - [obj freeMemory: p] && [obj freeMemory: q] && [obj freeMemory: r]) - - EXPECT_EXCEPTION(@"Detect out of memory on alloc", - OFOutOfMemoryException, [obj allocMemoryWithSize: SIZE_MAX]) - - EXPECT_EXCEPTION(@"Detect out of memory on resize", - OFOutOfMemoryException, - { - p = [obj allocMemoryWithSize: 1]; - [obj resizeMemory: p - toSize: SIZE_MAX]; - }) - [obj freeMemory: p]; - - TEST(@"Allocate when trying to resize NULL", - (p = [obj resizeMemory: NULL - toSize: 1024]) != NULL) - [obj freeMemory: p]; - - EXPECT_EXCEPTION(@"Detect resizing of memory not allocated by object", - OFMemoryNotPartOfObjectException, [obj resizeMemory: (void*)1 - toSize: 1024]) - - [pool release]; -} DELETED tests/plugin.m Index: tests/plugin.m ================================================================== --- tests/plugin.m +++ tests/plugin.m @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * 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 included in - * the packaging of this file. - */ - -#include "config.h" - -#import "OFPlugin.h" -#import "OFAutoreleasePool.h" -#import "OFString.h" -#import "OFExceptions.h" - -#import "main.h" - -#import "plugin/TestPlugin.h" - -static OFString *module = @"OFPlugin"; - -void -plugin_tests() -{ - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - TestPlugin *plugin; - - TEST(@"+[pluginFromFile:]", - (plugin = [OFPlugin pluginFromFile: @"plugin/TestPlugin"])) - - TEST(@"TestPlugin's -[test:]", [plugin test: 1234] == 2468) - - [pool release]; -} DELETED tests/string.m Index: tests/string.m ================================================================== --- tests/string.m +++ tests/string.m @@ -1,316 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * 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 included in - * the packaging of this file. - */ - -#include "config.h" - -#import "OFString.h" -#import "OFArray.h" -#import "OFAutoreleasePool.h" -#import "OFExceptions.h" - -#import "main.h" - -static OFString *module = @"OFString"; -static OFString* whitespace[] = { - @" \r \t\n\t \tasd \t \t\t\r\n", - @" \t\t \t\t \t \t" -}; - -@interface EntityHandler: OFObject -@end - -@implementation EntityHandler -- (OFString*)foundUnknownEntityNamed: (OFString*)entity -{ - if ([entity isEqual: @"foo"]) - return @"bar"; - - return nil; -} -@end - -void -string_tests() -{ - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - OFString *s[3]; - OFArray *a; - int i; - EntityHandler *h; - - s[0] = [OFMutableString stringWithString: @"täs€"]; - s[1] = [OFMutableString string]; - s[2] = [[s[0] copy] autorelease]; - - TEST(@"-[isEqual:]", [s[0] isEqual: s[2]] && - ![s[0] isEqual: [[[OFObject alloc] init] autorelease]]) - - TEST(@"-[compare:]", [s[0] compare: s[2]] == 0 && - [s[0] compare: @""] != 0) - - TEST(@"-[hash] is the same if -[isEqual:] is YES", - [s[0] hash] == [s[2] hash]) - - TEST(@"-[appendString:] and -[appendCString:]", - [s[1] appendCString: "1𝄞"] && [s[1] appendString: @"3"] && - [[s[0] appendString: s[1]] isEqual: @"täs€1𝄞3"]) - - TEST(@"-[length]", [s[0] length] == 7) - TEST(@"-[cStringLength]", [s[0] cStringLength] == 13) - TEST(@"-[hash]", [s[0] hash] == 0x8AC1EEF6) - - TEST(@"-[characterAtIndex:]", [s[0] characterAtIndex: 0] == 't' && - [s[0] characterAtIndex: 1] == 0xE4 && - [s[0] characterAtIndex: 3] == 0x20AC && - [s[0] characterAtIndex: 5] == 0x1D11E) - - EXPECT_EXCEPTION(@"Detect out of range in -[characterAtIndex:]", - OFOutOfRangeException, [s[0] characterAtIndex: 7]) - - TEST(@"-[reverse]", [[s[0] reverse] isEqual: @"3𝄞1€sät"]) - - s[1] = [OFMutableString stringWithString: @"abc"]; - - TEST(@"-[upper]", [[s[0] upper] isEqual: @"3𝄞1€SÄT"] && - [[s[1] upper] isEqual: @"ABC"]) - - TEST(@"-[lower]", [[s[0] lower] isEqual: @"3𝄞1€sät"] && - [[s[1] lower] isEqual: @"abc"]) - - TEST(@"+[stringWithCString:length:]", - (s[0] = [OFMutableString stringWithCString: "foobar" - length: 3]) && - [s[0] isEqual: @"foo"]) - - TEST(@"-[appendCStringWithLength:]", - [[s[0] appendCString: "foobarqux" + 3 - withLength: 3] isEqual: @"foobar"]) - - EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #1", - OFInvalidEncodingException, - [OFString stringWithCString: "\xE0\x80"]) - EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #2", - OFInvalidEncodingException, - [OFString stringWithCString: "\xF0\x80\x80\xC0"]) - - TEST(@"-[reverse] on UTF-8 strings", - (s[0] = [[OFMutableString stringWithCString: "äöü€𝄞"] reverse]) && - [s[0] isEqual: @"𝄞€üöä"]) - - TEST(@"Conversion of ISO 8859-1 to UTF-8", - [[OFString stringWithCString: "\xE4\xF6\xFC" - encoding: OF_STRING_ENCODING_ISO_8859_1] - isEqual: @"äöü"]) - - TEST(@"Conversion of ISO 8859-15 to UTF-8", - [[OFString stringWithCString: "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE" - encoding: OF_STRING_ENCODING_ISO_8859_15] - isEqual: @"€ŠšŽžŒœŸ"]) - - TEST(@"Conversion of Windows 1252 to UTF-8", - [[OFString stringWithCString: "\x80\x82\x83\x84\x85\x86\x87\x88" - "\x89\x8A\x8B\x8C\x8E\x91\x92\x93" - "\x94\x95\x96\x97\x98\x99\x9A\x9B" - "\x9C\x9E\x9F" - encoding: OF_STRING_ENCODING_WINDOWS_1252] - isEqual: @"€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ"]) - - TEST(@"+[stringWithFormat:]", - [(s[0] = [OFMutableString stringWithFormat: @"%s: %d", "test", 123]) - isEqual: @"test: 123"]) - - TEST(@"-[appendWithFormat:]", - [([s[0] appendWithFormat: @"%02X", 15]) isEqual: @"test: 1230F"]) - - TEST(@"-[indexOfFirstOccurrenceOfString:]", - [@"𝄞öö" indexOfFirstOccurrenceOfString: @"öö"] == 1 && - [@"𝄞öö" indexOfFirstOccurrenceOfString: @"ö"] == 1 && - [@"𝄞öö" indexOfFirstOccurrenceOfString: @"𝄞"] == 0 && - [@"𝄞öö" indexOfFirstOccurrenceOfString: @"x"] == SIZE_MAX) - - TEST(@"-[indexOfLastOccurrenceOfString:]", - [@"𝄞öö" indexOfLastOccurrenceOfString: @"öö"] == 1 && - [@"𝄞öö" indexOfLastOccurrenceOfString: @"ö"] == 2 && - [@"𝄞öö" indexOfLastOccurrenceOfString: @"𝄞"] == 0 && - [@"𝄞öö" indexOfLastOccurrenceOfString: @"x"] == SIZE_MAX) - - TEST(@"-[substringFromIndexToIndex:]", - [[@"𝄞öö" substringFromIndex: 1 - toIndex: 2] isEqual: @"ö"] && - [[@"𝄞öö" substringFromIndex: 3 - toIndex: 3] isEqual: @""]) - - EXPECT_EXCEPTION(@"Detect out of range in " - @"-[substringFromIndex:toIndex:] #1", OFOutOfRangeException, - [@"𝄞öö" substringFromIndex: 2 - toIndex: 4]) - EXPECT_EXCEPTION(@"Detect out of range in " - @"-[substringFromIndex:toIndex:] #2", OFOutOfRangeException, - [@"𝄞öö" substringFromIndex: 4 - toIndex: 4]) - - EXPECT_EXCEPTION(@"Detect start > end in " - @"-[substringFromIndex:toIndex:]", OFInvalidArgumentException, - [@"𝄞öö" substringFromIndex: 2 - toIndex: 0]) - - TEST(@"-[stringByAppendingString:]", - [[@"foo" stringByAppendingString: @"bar"] isEqual: @"foobar"]) - - TEST(@"-[hasPrefix:]", [@"foobar" hasPrefix: @"foo"] && - ![@"foobar" hasPrefix: @"foobar0"]) - - TEST(@"-[hasSuffix:]", [@"foobar" hasSuffix: @"bar"] && - ![@"foobar" hasSuffix: @"foobar0"]) - - i = 0; - TEST(@"-[splitWithDelimiter:]", - (a = [@"fooXXbarXXXXbazXXXX" splitWithDelimiter: @"XX"]) && - [[a objectAtIndex: i++] isEqual: @"foo"] && - [[a objectAtIndex: i++] isEqual: @"bar"] && - [[a objectAtIndex: i++] isEqual: @""] && - [[a objectAtIndex: i++] isEqual: @"baz"] && - [[a objectAtIndex: i++] isEqual: @""] && - [[a objectAtIndex: i++] isEqual: @""]) - - TEST(@"-[decimalValueAsInteger]", - [@"1234" decimalValueAsInteger] == 1234 && - [@"" decimalValueAsInteger] == 0) - - TEST(@"-[hexadecimalValueAsInteger]", - [@"123f" hexadecimalValueAsInteger] == 0x123f && - [@"0xABcd" hexadecimalValueAsInteger] == 0xABCD && - [@"xbCDE" hexadecimalValueAsInteger] == 0xBCDE && - [@"$CdEf" hexadecimalValueAsInteger] == 0xCDEF && - [@"" hexadecimalValueAsInteger] == 0) - - EXPECT_EXCEPTION(@"Detect invalid characters in " - @"-[decimalValueAsInteger] #1", OFInvalidEncodingException, - [@"abc" decimalValueAsInteger]) - EXPECT_EXCEPTION(@"Detect invalid characters in " - @"-[decimalValueAsInteger] #2", OFInvalidEncodingException, - [@"0a" decimalValueAsInteger]) - - EXPECT_EXCEPTION(@"Detect invalid chars in " - @"-[hexadecimalValueAsInteger] #1", OFInvalidEncodingException, - [@"0xABCDEFG" hexadecimalValueAsInteger]) - EXPECT_EXCEPTION(@"Detect invalid chars in " - @"-[hexadecimalValueAsInteger] #2", OFInvalidEncodingException, - [@"0x" hexadecimalValueAsInteger]) - EXPECT_EXCEPTION(@"Detect invalid chars in " - @"-[hexadecimalValueAsInteger] #3", OFInvalidEncodingException, - [@"$" hexadecimalValueAsInteger]) - - TEST(@"-[md5Hash]", [[@"asdfoobar" md5Hash] - isEqual: @"184dce2ec49b5422c7cfd8728864db4c"]) - - TEST(@"-[sha1Hash]", [[@"asdfoobar" sha1Hash] - isEqual: @"f5f81ac0a8b5cbfdc4585ec1ad32e7b3a12b9b49"]) - - TEST(@"-[stringByURLEncoding]", - [[@"foo\"ba'_~$" stringByURLEncoding] isEqual: @"foo%22ba%27_~%24"]) - - TEST(@"-[stringByURLDecoding]", - [[@"foo%20bar%22+%24" stringByURLDecoding] isEqual: @"foo bar\" $"]) - - EXPECT_EXCEPTION(@"Detect invalid encoding in -[stringByURLDecoding] " - @"#1", OFInvalidEncodingException, [@"foo%bar" stringByURLDecoding]) - EXPECT_EXCEPTION(@"Detect invalid encoding in -[stringByURLDecoding] " - @"#2", OFInvalidEncodingException, - [@"foo%FFbar" stringByURLDecoding]) - - TEST(@"-[removeCharactersFromIndex:toIndex:]", - (s[0] = [OFMutableString stringWithString: @"𝄞öööbä€"]) && - [s[0] removeCharactersFromIndex: 1 - toIndex: 4] && - [s[0] isEqual: @"𝄞bä€"] && - [s[0] removeCharactersFromIndex: 0 - toIndex: 4] && - [s[0] isEqual: @""]) - - EXPECT_EXCEPTION(@"Detect OoR in " - @"-[removeCharactersFromIndex:toIndex:] #1", OFOutOfRangeException, - { - s[0] = [OFMutableString stringWithString: @"𝄞öö"]; - [s[0] substringFromIndex: 2 - toIndex: 4]; - }) - - EXPECT_EXCEPTION(@"Detect OoR in " - @"-[removeCharactersFromIndex:toIndex:] #2", OFOutOfRangeException, - [s[0] substringFromIndex: 4 - toIndex: 4]) - - EXPECT_EXCEPTION(@"Detect s > e in " - @"-[removeCharactersFromIndex:toIndex:]", - OFInvalidArgumentException, - [s[0] substringFromIndex: 2 - toIndex: 0]) - - TEST(@"-[replaceOccurrencesOfString:withString:]", - [[[OFMutableString stringWithString: @"asd fo asd fofo asd"] - replaceOccurrencesOfString: @"fo" - withString: @"foo"] - isEqual: @"asd foo asd foofoo asd"] && - [[[OFMutableString stringWithString: @"XX"] - replaceOccurrencesOfString: @"X" - withString: @"XX"] - isEqual: @"XXXX"]) - - TEST(@"-[removeLeadingWhitespaces]", - (s[0] = [OFMutableString stringWithString: whitespace[0]]) && - [[s[0] removeLeadingWhitespaces] isEqual: @"asd \t \t\t\r\n"] && - (s[0] = [OFMutableString stringWithString: whitespace[1]]) && - [[s[0] removeLeadingWhitespaces] isEqual: @""]) - - TEST(@"-[removeTrailingWhitespaces]", - (s[0] = [OFMutableString stringWithString: whitespace[0]]) && - [[s[0] removeTrailingWhitespaces] isEqual: @" \r \t\n\t \tasd"] && - (s[0] = [OFMutableString stringWithString: whitespace[1]]) && - [[s[0] removeTrailingWhitespaces] isEqual: @""]) - - TEST(@"-[removeLeadingAndTrailingWhitespaces]", - (s[0] = [OFMutableString stringWithString: whitespace[0]]) && - [[s[0] removeLeadingAndTrailingWhitespaces] isEqual: @"asd"] && - (s[0] = [OFMutableString stringWithString: whitespace[1]]) && - [[s[0] removeLeadingAndTrailingWhitespaces] isEqual: @""]) - - TEST(@"-[stringByXMLEscaping]", - (s[0] = [@" &world'\"!&" stringByXMLEscaping]) && - [s[0] isEqual: @"<hello> &world'"!&"]) - - TEST(@"-[stringByXMLUnescaping]", - [[s[0] stringByXMLUnescaping] isEqual: @" &world'\"!&"] && - [[@"y" stringByXMLUnescaping] isEqual: @"y"] && - [[@"ä" stringByXMLUnescaping] isEqual: @"ä"] && - [[@"€" stringByXMLUnescaping] isEqual: @"€"] && - [[@"𝄞" stringByXMLUnescaping] isEqual: @"𝄞"]) - - EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " - @"#1", OFInvalidEncodingException, [@"&foo;" stringByXMLUnescaping]) - EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " - @"#2", OFInvalidEncodingException, [@"x&" stringByXMLUnescaping]) - EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " - @"#3", OFInvalidEncodingException, [@"&#;" stringByXMLUnescaping]) - EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " - @"#4", OFInvalidEncodingException, [@"&#x;" stringByXMLUnescaping]) - EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " - @"#5", OFInvalidEncodingException, [@"&#g;" stringByXMLUnescaping]) - EXPECT_EXCEPTION(@"Detect invalid entities in -[stringByXMLUnescaping] " - @"#6", OFInvalidEncodingException, [@"&#xg;" stringByXMLUnescaping]) - - TEST(@"-[stringByXMLUnescapingWithHandler:]", - (h = [[[EntityHandler alloc] init] autorelease]) && - (s[0] = [@"x&foo;y" stringByXMLUnescapingWithHandler: h]) && - [s[0] isEqual: @"xbary"]) - - [pool release]; -} DELETED tests/tcpsocket.m Index: tests/tcpsocket.m ================================================================== --- tests/tcpsocket.m +++ tests/tcpsocket.m @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * 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 included in - * the packaging of this file. - */ - -#include "config.h" - -#include -#include -#include - -#import "OFTCPSocket.h" -#import "OFAutoreleasePool.h" -#import "OFString.h" -#import "OFExceptions.h" -#import "OFMacros.h" - -#import "main.h" - -static OFString *module = @"OFTCPSocket"; - -void -tcpsocket_tests() -{ - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - OFSocket *server, *client = nil, *accepted; - OFString *service, *msg; - uint16_t port; - char buf[6]; - - srand(time(NULL)); - port = (uint16_t)rand(); - if (port < 1024) - port += 1024; - service = [OFString stringWithFormat: @"%d", port]; - - TEST(@"+[socket]", (server = [OFTCPSocket socket]) && - (client = [OFTCPSocket socket])) - - msg = [OFString stringWithFormat: - @"-[bindService:onNode:withFamily:] (port %d)", port]; - TEST(msg, [server bindService: service - onNode: @"localhost" - withFamily: AF_INET]) - - TEST(@"-[listen]", [server listen]) - - TEST(@"-[connectToService:onNode:]", - [client connectToService: service - onNode: @"localhost"]) - - TEST(@"-[accept]", (accepted = [server accept])) - - TEST(@"-[writeString:]", [client writeString: @"Hello!"]) - - TEST(@"-[readNBytes:intoBuffer:]", [accepted readNBytes: 6 - intoBuffer: buf] && - !memcmp(buf, "Hello!", 6)) - - [pool release]; -} DELETED tests/thread.m Index: tests/thread.m ================================================================== --- tests/thread.m +++ tests/thread.m @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * 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 included in - * the packaging of this file. - */ - -#include "config.h" - -#import "OFThread.h" -#import "OFAutoreleasePool.h" -#import "OFString.h" -#import "OFExceptions.h" - -#import "main.h" - -static OFString *module = @"OFThread"; - -@interface TestThread: OFThread -@end - -@implementation TestThread -- main -{ - if ([object isEqual: @"foo"]) - return @"success"; - - return nil; -} -@end - -void -thread_tests() -{ - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - TestThread *t; - OFTLSKey *key; - - TEST(@"+[threadWithObject:]", - (t = [TestThread threadWithObject: @"foo"])) - - TEST(@"-[join]", [[t join] isEqual: @"success"]) - - TEST(@"OFTLSKey's +[tlsKey]", (key = [OFTLSKey tlsKey])) - - TEST(@"+[setObject:forTLSKey:]", [OFThread setObject: @"foo" - forTLSKey: key]) - - TEST(@"+[objectForTLSKey:]", - [[OFThread objectForTLSKey: key] isEqual: @"foo"]) - - [pool release]; -} DELETED tests/xmlelement.m Index: tests/xmlelement.m ================================================================== --- tests/xmlelement.m +++ tests/xmlelement.m @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * 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 included in - * the packaging of this file. - */ - -#include "config.h" - -#import "OFXMLElement.h" -#import "OFAutoreleasePool.h" -#import "OFString.h" -#import "OFExceptions.h" - -#import "main.h" - -static OFString *module = @"OFXMLElement"; - -void -xmlelement_tests() -{ - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - OFXMLElement *elem[2]; - - TEST(@"+[elementWithName:]", - (elem[0] = [OFXMLElement elementWithName: @"foo"]) && - [[elem[0] string] isEqual: @""]) - - TEST(@"+[elementWithName:stringValue:]", - (elem[1] = [OFXMLElement elementWithName: @"foo" - stringValue: @"b&ar"]) && - [[elem[1] string] isEqual: @"b&ar"]) - - TEST(@"-[addAttributeWithName:stringValue:]", - [elem[0] addAttributeWithName: @"foo" - stringValue: @"b&ar"] && - [[elem[0] string] isEqual: @""] && - [elem[1] addAttributeWithName: @"foo" - stringValue: @"b&ar"] && - [[elem[1] string] isEqual: @"b&ar"]) - - TEST(@"-[addChild:]", - [elem[0] addChild: [OFXMLElement elementWithName: @"bar"]] && - [[elem[0] string] isEqual: @""]) - - [pool release]; -} DELETED tests/xmlparser.m Index: tests/xmlparser.m ================================================================== --- tests/xmlparser.m +++ tests/xmlparser.m @@ -1,190 +0,0 @@ -/* - * Copyright (c) 2008 - 2009 - * 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 included in - * the packaging of this file. - */ - -#include "config.h" - -#include - -#import "OFXMLParser.h" -#import "OFAutoreleasePool.h" -#import "OFString.h" -#import "OFExceptions.h" - -#import "main.h" - -static OFString *module = @"OFXMLParser"; -static int i = 0; - -enum event_type { - TAG_START, - TAG_END, - STRING, - COMMENT -}; - -static void -callback(enum event_type et, OFString *name, OFString *prefix, OFString *ns, - OFArray *attrs, OFString *string, OFString *comment) -{ - OFString *msg; - id *carray; - size_t count; - - i++; - msg = [OFString stringWithFormat: @"Parsing part #%d", i]; - - switch (i) { - case 1: - case 5: - TEST(msg, et == STRING && [string isEqual: @"bar"]) - break; - case 2: - /* FIXME: Namespace */ - carray = [attrs cArray]; - count = [attrs count]; - - TEST(msg, et == TAG_START && [name isEqual: @"bar"] && - [prefix isEqual: @"foo"] && ns == nil && - attrs != nil && count == 2 && - /* Attribute 1 */ - [[carray[0] name] isEqual: @"bar"] && - [carray[0] prefix] == nil && - [[carray[0] stringValue] isEqual: @"b&az"] && - [carray[0] namespace] == nil && - /* Attribute 2 */ - [[carray[1] name] isEqual: @"qux"] && - [[carray[1] prefix] isEqual: @"qux"] && - [[carray[1] stringValue] isEqual: @" quux "] && - [carray[1] namespace] == nil) - break; - case 3: - TEST(msg, et == STRING && [string isEqual: @"foo\r\n" - "foo<barbar quxbar\r\n" - ""; - size_t j, len; - - TEST(@"+[xmlParser]", (parser = [OFXMLParser xmlParser])) - - TEST(@"-[setDelegate:]", - [parser setDelegate: [[[ParserDelegate alloc] init] autorelease]]) - - /* Simulate a stream where we only get chunks */ - len = strlen(str); - - for (j = 0; j < len; j+= 2) { - if (j + 2 > len) - [parser parseBuffer: str + j - withSize: 1]; - else - [parser parseBuffer: str + j - withSize: 2]; - } - - TEST(@"Checking if everything was parsed", i == 11) - - [pool release]; -}