1 | /*
|
---|
2 | * Copyright (c) 2015 Michal Koutny
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <adt/dyn_array.h>
|
---|
30 | #include <assert.h>
|
---|
31 | #include <pcut/pcut.h>
|
---|
32 |
|
---|
33 | PCUT_INIT
|
---|
34 |
|
---|
35 | PCUT_TEST_SUITE(dyn_array);
|
---|
36 |
|
---|
37 | typedef int data_t;
|
---|
38 | static dyn_array_t da;
|
---|
39 |
|
---|
40 | PCUT_TEST_BEFORE {
|
---|
41 | int rc = dyn_array_initialize(&da, data_t, 3);
|
---|
42 | assert(rc == EOK);
|
---|
43 | }
|
---|
44 |
|
---|
45 | PCUT_TEST_AFTER {
|
---|
46 | dyn_array_destroy(&da);
|
---|
47 | }
|
---|
48 |
|
---|
49 | PCUT_TEST(initialization) {
|
---|
50 | PCUT_ASSERT_INT_EQUALS(da.capacity, 3);
|
---|
51 | PCUT_ASSERT_INT_EQUALS(da.size, 0);
|
---|
52 | }
|
---|
53 |
|
---|
54 | PCUT_TEST(append) {
|
---|
55 | dyn_array_append(&da, data_t, 42);
|
---|
56 | dyn_array_append(&da, data_t, 666);
|
---|
57 |
|
---|
58 | PCUT_ASSERT_INT_EQUALS(2, da.size);
|
---|
59 | PCUT_ASSERT_INT_EQUALS(42, dyn_array_at(&da, data_t, 0));
|
---|
60 | PCUT_ASSERT_INT_EQUALS(666, dyn_array_at(&da, data_t, 1));
|
---|
61 | }
|
---|
62 |
|
---|
63 | PCUT_TEST(assign) {
|
---|
64 | dyn_array_append(&da, data_t, 42);
|
---|
65 | dyn_array_at(&da, data_t, 0) = 112;
|
---|
66 |
|
---|
67 | PCUT_ASSERT_INT_EQUALS(112, dyn_array_at(&da, data_t, 0));
|
---|
68 | }
|
---|
69 |
|
---|
70 | PCUT_TEST(remove) {
|
---|
71 | dyn_array_append(&da, data_t, 10);
|
---|
72 | dyn_array_append(&da, data_t, 11);
|
---|
73 |
|
---|
74 | dyn_array_remove(&da, 0);
|
---|
75 |
|
---|
76 | PCUT_ASSERT_INT_EQUALS(1, da.size);
|
---|
77 | PCUT_ASSERT_INT_EQUALS(11, dyn_array_at(&da, data_t, 0));
|
---|
78 | }
|
---|
79 |
|
---|
80 | PCUT_TEST(insert) {
|
---|
81 | dyn_array_append(&da, data_t, 10);
|
---|
82 | dyn_array_append(&da, data_t, 11);
|
---|
83 | dyn_array_append(&da, data_t, 12);
|
---|
84 | dyn_array_insert(&da, data_t, 1, 99);
|
---|
85 |
|
---|
86 | PCUT_ASSERT_INT_EQUALS(4, da.size);
|
---|
87 | PCUT_ASSERT_INT_EQUALS(10, dyn_array_at(&da, data_t, 0));
|
---|
88 | PCUT_ASSERT_INT_EQUALS(99, dyn_array_at(&da, data_t, 1));
|
---|
89 | PCUT_ASSERT_INT_EQUALS(11, dyn_array_at(&da, data_t, 2));
|
---|
90 | PCUT_ASSERT_INT_EQUALS(12, dyn_array_at(&da, data_t, 3));
|
---|
91 | }
|
---|
92 |
|
---|
93 | PCUT_TEST(capacity_grow) {
|
---|
94 | dyn_array_append(&da, data_t, 42);
|
---|
95 | dyn_array_append(&da, data_t, 666);
|
---|
96 | dyn_array_append(&da, data_t, 42);
|
---|
97 | dyn_array_append(&da, data_t, 666);
|
---|
98 |
|
---|
99 | PCUT_ASSERT_TRUE(da.capacity > 3);
|
---|
100 | }
|
---|
101 |
|
---|
102 | PCUT_TEST(capacity_shrink) {
|
---|
103 | dyn_array_append(&da, data_t, 42);
|
---|
104 | dyn_array_append(&da, data_t, 666);
|
---|
105 | dyn_array_append(&da, data_t, 42);
|
---|
106 |
|
---|
107 | dyn_array_remove(&da, 0);
|
---|
108 | dyn_array_remove(&da, 0);
|
---|
109 | dyn_array_remove(&da, 0);
|
---|
110 |
|
---|
111 | PCUT_ASSERT_TRUE(da.capacity < 3);
|
---|
112 | }
|
---|
113 |
|
---|
114 | PCUT_TEST(iterator) {
|
---|
115 | for (int i = 0; i < 10; ++i) {
|
---|
116 | dyn_array_append(&da, data_t, i*i);
|
---|
117 | }
|
---|
118 |
|
---|
119 | int i = 0;
|
---|
120 | dyn_array_foreach(da, data_t, it) {
|
---|
121 | PCUT_ASSERT_INT_EQUALS(i*i, *it);
|
---|
122 | ++i;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | PCUT_EXPORT(dyn_array);
|
---|