aboutsummaryrefslogtreecommitdiff
path: root/src/unit/array.unit
blob: cb1b1e5aaffad37104c78007fd6d9bb03b6c76b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// vim: syntax=cpp
/*
 * Copyright (C) 2007-2023 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/hash.h"
#include "bu/array.h"

#include "bu/sio.h"
using Bu::sio;

suite Array
{
    test general
    {
        Bu::Array<int> ai;

        ai.append( 5 );
        ai.append( 10 );
        unitTest( ai.getSize() == 2 );
        unitTest( ai.getCapacity() == 10 );
        unitTest( ai[0] == 5 );
        unitTest( ai[1] == 10 );
    }

    test iterate1
    {
        Bu::Array<int> ai;
        for( int j = 0; j < 10; j++ )
            ai.append( j );

        int j = 0;
        for( Bu::Array<int>::iterator i = ai.begin(); i != ai.end(); i++ )
            unitTest( (*i) == j++ );
        unitTest( j == 10 );
        
        const Bu::Array<int> &ci = ai;
        j = 0;
        for( Bu::Array<int>::const_iterator i = ci.begin(); i; i++ )
            unitTest( (*i) == j++ );
        unitTest( j == 10 );
    }

    test iterate2
    {
        Bu::Array<int> ai;
        for( Bu::Array<int>::iterator i = ai.begin(); i != ai.end(); i++ )
            unitFailed("Empty lists shouldn't be iterated through.");
        for( Bu::Array<int>::iterator i = ai.begin(); i; i++ )
            unitFailed("Empty lists shouldn't be iterated through.");
    }

    test copy
    {
        typedef Bu::Hash<Bu::String, Bu::String> StrHash;
        typedef Bu::Array<StrHash> StrHashArray;

        StrHash h1;
        h1["Hi"] = "Yo";
        h1["Bye"] = "Later";

        StrHash h2;
        h2["Test"] = "Bloop";
        h2["Foo"] = "ooF";

        StrHashArray a1;
        a1.append( h1 );
        a1.append( h2 );

        StrHashArray a2(a1);

        unitTest( a2[0].get("Hi") == "Yo" );
        unitTest( a2[0].get("Bye") == "Later" );
        unitTest( a2[1].get("Test") == "Bloop" );
        unitTest( a2[1].get("Foo") == "ooF" );

        StrHashArray a3;
        a3 = a1;
        
        unitTest( a3[0].get("Hi") == "Yo" );
        unitTest( a3[0].get("Bye") == "Later" );
        unitTest( a3[1].get("Test") == "Bloop" );
        unitTest( a3[1].get("Foo") == "ooF" );
    }

    test insert
    {
        Bu::Array<int> aInts;
        aInts.insert( aInts.end(), 4 );
        aInts.insert( aInts.begin(), 1 );
        aInts.insert( aInts.end(), 5 );
        aInts.insert( aInts.begin()+1, 3 );
        aInts.insert( aInts.begin()+1, 2 );
        aInts.insert( aInts.begin(), 0 );

        for( int j = 0; j < 6; j++ )
        {
            unitTest( aInts[j] == j );
        }
    }

    test setSize
    {
        Bu::Array<Bu::String> aStr( 3 );
        aStr.setSize( 3 );
        aStr[1] = "Hello";
        aStr[0] = aStr[1].clone();
    }

    test swap
    {
        Bu::Array<Bu::String> aStr( 3 );
        aStr.append("One");
        aStr.append("Three");
        aStr.append("Two");

        aStr.swap( 1, 2 );
        unitTest( aStr[1] == "Two" );
        unitTest( aStr[2] == "Three");
    }
}