/*
* Copyright (c) 2008 - 2009
* Jonathan Schleifer <js@webkeks.org>
*
* 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 <string.h>
#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[4];
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]])
array[2] = [OFDataArray dataArrayWithItemSize: 1];
array[3] = [OFDataArray dataArrayWithItemSize: 1];
[array[2] addItem: "a"];
[array[2] addItem: "a"];
[array[3] addItem: "z"];
TEST(@"-[compare]", [array[0] compare: array[1]] == 0 &&
[array[1] removeNItems: 1] &&
[array[0] compare: array[1]] == OF_ORDERED_DESCENDING &&
[array[1] compare: array[0]] == OF_ORDERED_ASCENDING &&
[array[2] compare: array[3]] == OF_ORDERED_ASCENDING)
TEST(@"-[hash]", [array[0] hash] == 0xC54621B6)
array[0] = [class dataArrayWithItemSize: 1];
[array[0] addNItems: 6
fromCArray: "abcdef"];
TEST(@"-[removeNItems:]", [array[0] removeNItems: 1] &&
[array[0] count] == 5 &&
!memcmp([array[0] cArray], "abcde", 5))
TEST(@"-[removeNItems:atIndex:]",
[array[0] removeNItems: 2
atIndex: 1] && [array[0] count] == 3 &&
!memcmp([array[0] cArray], "ade", 3))
[array[0] addItem: ""];
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 drain];
}