source: mainline/uspace/lib/c/test/dyn_array.c@ dda2602

Last change on this file since dda2602 was 4b1c6a4b, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

libc: Add more dyn_array functions
Conflicts:

boot/Makefile.common

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[62d3d87]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
33PCUT_INIT
34
35PCUT_TEST_SUITE(dyn_array);
36
37typedef int data_t;
38static dyn_array_t da;
39
40PCUT_TEST_BEFORE {
41 int rc = dyn_array_initialize(&da, data_t, 3);
42 assert(rc == EOK);
43}
44
45PCUT_TEST_AFTER {
46 dyn_array_destroy(&da);
47}
48
49PCUT_TEST(initialization) {
50 PCUT_ASSERT_INT_EQUALS(da.capacity, 3);
51 PCUT_ASSERT_INT_EQUALS(da.size, 0);
52}
53
54PCUT_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
63PCUT_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
70PCUT_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
80PCUT_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
93PCUT_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
102PCUT_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
114PCUT_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
[4b1c6a4b]126PCUT_TEST(find) {
127 dyn_array_append(&da, data_t, 10);
128 dyn_array_append(&da, data_t, 11);
129 dyn_array_append(&da, data_t, 12);
130 dyn_array_append(&da, data_t, 99);
131
132 PCUT_ASSERT_INT_EQUALS(0, dyn_array_find(&da, data_t, 10));
133 PCUT_ASSERT_INT_EQUALS(3, dyn_array_find(&da, data_t, 99));
134 PCUT_ASSERT_INT_EQUALS(4, dyn_array_find(&da, data_t, 666));
135}
[62d3d87]136
137PCUT_EXPORT(dyn_array);
Note: See TracBrowser for help on using the repository browser.