Index: new_tests/Makefile ================================================================== --- new_tests/Makefile +++ new_tests/Makefile @@ -33,10 +33,11 @@ OFInvocationTests.m \ OFJSONTests.m \ OFListTests.m \ OFLocaleTests.m \ OFMatrix4x4Tests.m \ + OFMemoryStreamTests.m \ OFMethodSignatureTests.m \ OFMutableArrayTests.m \ OFMutableDataTests.m \ OFMutableDictionaryTests.m \ OFMutableSetTests.m \ ADDED new_tests/OFMemoryStreamTests.m Index: new_tests/OFMemoryStreamTests.m ================================================================== --- new_tests/OFMemoryStreamTests.m +++ new_tests/OFMemoryStreamTests.m @@ -0,0 +1,85 @@ +/* + * 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" + +#import "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFMemoryStreamTests: OTTestCase +@end + +static const char string[] = "abcdefghijkl"; + +@implementation OFMemoryStreamTests +- (void)testReadOnlyMemoryStream +{ + OFMemoryStream *stream = [OFMemoryStream + streamWithMemoryAddress: (char *)string + size: sizeof(string) + writable: false]; + char buffer[10]; + + /* + * Test the lowlevel methods, as otherwise OFStream will do one big + * read and we will not test OFMemoryStream. + */ + + OTAssertEqual([stream lowlevelReadIntoBuffer: buffer length: 5], 5); + OTAssertEqual(memcmp(buffer, "abcde", 5), 0); + OTAssertEqual([stream lowlevelReadIntoBuffer: buffer length: 3], 3); + OTAssertEqual(memcmp(buffer, "fgh", 3), 0); + OTAssertEqual([stream lowlevelReadIntoBuffer: buffer length: 10], 5); + OTAssertEqual(memcmp(buffer, "ijkl", 5), 0); + OTAssertTrue([stream lowlevelIsAtEndOfStream]); + + OTAssertEqual([stream lowlevelSeekToOffset: 0 whence: OFSeekCurrent], + sizeof(string)); + OTAssertTrue([stream lowlevelIsAtEndOfStream]); + + OTAssertEqual([stream lowlevelSeekToOffset: 4 whence: OFSeekSet], 4); + OTAssertFalse([stream lowlevelIsAtEndOfStream]); + OTAssertEqual([stream lowlevelReadIntoBuffer: buffer length: 10], 9); + OTAssertEqual(memcmp(buffer, "efghijkl", 9), 0); + + OTAssertEqual([stream lowlevelSeekToOffset: -2 whence: OFSeekEnd], 11); + OTAssertEqual([stream lowlevelReadIntoBuffer: buffer length: 10], 2); + OTAssertEqual(memcmp(buffer, "l", 2), 0); + OTAssertEqual([stream lowlevelReadIntoBuffer: buffer length: 10], 0); + + OTAssertThrowsSpecific([stream lowlevelWriteBuffer: "" length: 1], + OFWriteFailedException); +} + +- (void)testReadWriteMemoryStream +{ + OFMutableData *data = [OFMutableData dataWithCapacity: 13]; + OFMemoryStream *stream; + + [data increaseCountBy: 13]; + stream = [OFMemoryStream streamWithMemoryAddress: data.mutableItems + size: data.count + writable: true]; + + OTAssertEqual([stream lowlevelWriteBuffer: "abcde" length: 5], 5); + OTAssertEqual([stream lowlevelWriteBuffer: "fgh" length: 3], 3); + OTAssertEqual([stream lowlevelWriteBuffer: "ijkl" length: 5], 5); + OTAssertEqual(memcmp(data.items, string, data.count), 0); + OTAssertEqual([stream lowlevelSeekToOffset: -3 whence: OFSeekEnd], 10); + + OTAssertThrowsSpecific([stream lowlevelWriteBuffer: "xyz" length: 4], + OFWriteFailedException); +} +@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 = OFMemoryStreamTests.m \ - OFStreamTests.m \ +SRCS = OFStreamTests.m \ OFValueTests.m \ OFXMLNodeTests.m \ OFXMLParserTests.m \ TestsAppDelegate.m DELETED tests/OFMemoryStreamTests.m Index: tests/OFMemoryStreamTests.m ================================================================== --- tests/OFMemoryStreamTests.m +++ tests/OFMemoryStreamTests.m @@ -1,87 +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" - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFMemoryStream"; -static const char string[] = "abcdefghijkl"; - -@implementation TestsAppDelegate (OFMemoryStreamTests) -- (void)memoryStreamTests -{ - void *pool = objc_autoreleasePoolPush(); - OFMemoryStream *stream; - char buffer[10]; - OFMutableData *data; - - TEST(@"+[streamWithMemoryAddress:size:writable:]", - (stream = [OFMemoryStream streamWithMemoryAddress: (char *)string - size: sizeof(string) - writable: false])); - - /* - * Test the lowlevel methods, as otherwise OFStream will do one big - * read and we will not test OFMemoryStream. - */ - - TEST(@"-[lowlevelReadIntoBuffer:length:]", - [stream lowlevelReadIntoBuffer: buffer length: 5] == 5 && - memcmp(buffer, "abcde", 5) == 0 && - [stream lowlevelReadIntoBuffer: buffer length: 3] == 3 && - memcmp(buffer, "fgh", 3) == 0 && - [stream lowlevelReadIntoBuffer: buffer length: 10] == 5 && - memcmp(buffer, "ijkl", 5) == 0) - - TEST(@"-[lowlevelIsAtEndOfStream]", [stream lowlevelIsAtEndOfStream]) - - TEST(@"-[lowlevelSeekToOffset:whence:]", - [stream lowlevelSeekToOffset: 0 whence: OFSeekCurrent] == - sizeof(string) && [stream lowlevelIsAtEndOfStream] && - [stream lowlevelSeekToOffset: 4 whence: OFSeekSet] == 4 && - ![stream lowlevelIsAtEndOfStream] && - [stream lowlevelReadIntoBuffer: buffer length: 10] == 9 && - memcmp(buffer, "efghijkl", 9) == 0 && - [stream lowlevelSeekToOffset: -2 whence: OFSeekEnd] == 11 && - [stream lowlevelReadIntoBuffer: buffer length: 10] == 2 && - memcmp(buffer, "l", 2) == 0 && - [stream lowlevelReadIntoBuffer: buffer length: 10] == 0) - - EXPECT_EXCEPTION(@"Writes rejected on read-only stream", - OFWriteFailedException, [stream lowlevelWriteBuffer: "" length: 1]) - - data = [OFMutableData dataWithCapacity: 13]; - [data increaseCountBy: 13]; - stream = [OFMemoryStream streamWithMemoryAddress: data.mutableItems - size: data.count - writable: true]; - TEST(@"-[lowlevelWriteBuffer:length:]", - [stream lowlevelWriteBuffer: "abcde" length: 5] == 5 && - [stream lowlevelWriteBuffer: "fgh" length: 3] == 3 && - [stream lowlevelWriteBuffer: "ijkl" length: 5] == 5 && - memcmp(data.items, string, data.count) == 0 && - [stream lowlevelSeekToOffset: -3 whence: OFSeekEnd] == 10) - - EXPECT_EXCEPTION(@"Out of bound writes rejected", - OFWriteFailedException, - [stream lowlevelWriteBuffer: "xyz" length: 4]) - - TEST(@"Partial write for too long write", - memcmp(data.items, "abcdefghijxyz", 13) == 0) - - objc_autoreleasePoolPop(pool); -} -@end Index: tests/TestsAppDelegate.h ================================================================== --- tests/TestsAppDelegate.h +++ tests/TestsAppDelegate.h @@ -57,18 +57,10 @@ - (void)outputTesting: (OFString *)test inModule: (OFString *)module; - (void)outputSuccess: (OFString *)test inModule: (OFString *)module; - (void)outputFailure: (OFString *)test inModule: (OFString *)module; @end -@interface TestsAppDelegate (OFDataTests) -- (void)dataTests; -@end - -@interface TestsAppDelegate (OFMemoryStreamTests) -- (void)memoryStreamTests; -@end - @interface TestsAppDelegate (OFStreamTests) - (void)streamTests; @end @interface TestsAppDelegate (OFValueTests) Index: tests/TestsAppDelegate.m ================================================================== --- tests/TestsAppDelegate.m +++ tests/TestsAppDelegate.m @@ -368,14 +368,12 @@ #if defined(OF_WII) && defined(OF_HAVE_FILES) [[OFFileManager defaultManager] changeCurrentDirectoryPath: @"/apps/objfw-tests"]; #endif - [self dataTests]; [self valueTests]; [self streamTests]; - [self memoryStreamTests]; [self XMLParserTests]; [self XMLNodeTests]; [OFStdOut reset];