Lucas De Marchi | fdafa6b | 2014-10-08 01:12:15 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
Lucas De Marchi | dea2dfe | 2014-12-25 23:32:03 -0200 | [diff] [blame] | 15 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
Lucas De Marchi | fdafa6b | 2014-10-08 01:12:15 -0300 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <stddef.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <shared/array.h> |
| 26 | |
| 27 | #include "testsuite.h" |
| 28 | |
| 29 | static int test_array_append1(const struct test *t) |
| 30 | { |
| 31 | struct array array; |
| 32 | const char *c1 = "test1"; |
| 33 | |
| 34 | array_init(&array, 2); |
| 35 | array_append(&array, c1); |
| 36 | assert_return(array.count == 1, EXIT_FAILURE); |
| 37 | assert_return(array.array[0] == c1, EXIT_FAILURE); |
| 38 | array_free_array(&array); |
| 39 | |
| 40 | return 0; |
| 41 | } |
Lucas De Marchi | f1155c1 | 2014-10-09 13:00:30 -0300 | [diff] [blame] | 42 | DEFINE_TEST(test_array_append1, |
Lucas De Marchi | fdafa6b | 2014-10-08 01:12:15 -0300 | [diff] [blame] | 43 | .description = "test simple array append"); |
| 44 | |
| 45 | |
| 46 | static int test_array_append2(const struct test *t) |
| 47 | { |
| 48 | struct array array; |
| 49 | const char *c1 = "test1"; |
| 50 | const char *c2 = "test2"; |
| 51 | const char *c3 = "test3"; |
| 52 | |
| 53 | array_init(&array, 2); |
| 54 | array_append(&array, c1); |
| 55 | array_append(&array, c2); |
| 56 | array_append(&array, c3); |
| 57 | assert_return(array.count == 3, EXIT_FAILURE); |
| 58 | assert_return(array.array[0] == c1, EXIT_FAILURE); |
| 59 | assert_return(array.array[1] == c2, EXIT_FAILURE); |
| 60 | assert_return(array.array[2] == c3, EXIT_FAILURE); |
| 61 | array_free_array(&array); |
| 62 | |
| 63 | return 0; |
| 64 | } |
Lucas De Marchi | f1155c1 | 2014-10-09 13:00:30 -0300 | [diff] [blame] | 65 | DEFINE_TEST(test_array_append2, |
Lucas De Marchi | fdafa6b | 2014-10-08 01:12:15 -0300 | [diff] [blame] | 66 | .description = "test array append over step"); |
| 67 | |
| 68 | static int test_array_append_unique(const struct test *t) |
| 69 | { |
| 70 | struct array array; |
| 71 | const char *c1 = "test1"; |
| 72 | const char *c2 = "test2"; |
| 73 | const char *c3 = "test3"; |
| 74 | |
| 75 | array_init(&array, 2); |
| 76 | array_append_unique(&array, c1); |
| 77 | array_append_unique(&array, c2); |
| 78 | array_append_unique(&array, c3); |
| 79 | array_append_unique(&array, c3); |
| 80 | array_append_unique(&array, c2); |
| 81 | array_append_unique(&array, c1); |
| 82 | assert_return(array.count == 3, EXIT_FAILURE); |
| 83 | assert_return(array.array[0] == c1, EXIT_FAILURE); |
| 84 | assert_return(array.array[1] == c2, EXIT_FAILURE); |
| 85 | assert_return(array.array[2] == c3, EXIT_FAILURE); |
| 86 | array_free_array(&array); |
| 87 | |
| 88 | return 0; |
| 89 | } |
Lucas De Marchi | f1155c1 | 2014-10-09 13:00:30 -0300 | [diff] [blame] | 90 | DEFINE_TEST(test_array_append_unique, |
Lucas De Marchi | fdafa6b | 2014-10-08 01:12:15 -0300 | [diff] [blame] | 91 | .description = "test array append unique"); |
| 92 | |
| 93 | static int test_array_sort(const struct test *t) |
| 94 | { |
| 95 | struct array array; |
| 96 | const char *c1 = "test1"; |
| 97 | const char *c2 = "test2"; |
| 98 | const char *c3 = "test3"; |
| 99 | |
| 100 | array_init(&array, 2); |
| 101 | array_append(&array, c1); |
| 102 | array_append(&array, c2); |
| 103 | array_append(&array, c3); |
| 104 | array_append(&array, c2); |
| 105 | array_append(&array, c3); |
| 106 | array_append(&array, c1); |
| 107 | array_sort(&array, (int (*)(const void *a, const void *b)) strcmp); |
| 108 | assert_return(array.count == 6, EXIT_FAILURE); |
| 109 | assert_return(array.array[0] == c1, EXIT_FAILURE); |
| 110 | assert_return(array.array[1] == c1, EXIT_FAILURE); |
| 111 | assert_return(array.array[2] == c2, EXIT_FAILURE); |
| 112 | assert_return(array.array[3] == c2, EXIT_FAILURE); |
| 113 | assert_return(array.array[4] == c3, EXIT_FAILURE); |
| 114 | assert_return(array.array[5] == c3, EXIT_FAILURE); |
| 115 | array_free_array(&array); |
| 116 | |
| 117 | return 0; |
| 118 | } |
Lucas De Marchi | f1155c1 | 2014-10-09 13:00:30 -0300 | [diff] [blame] | 119 | DEFINE_TEST(test_array_sort, |
Lucas De Marchi | fdafa6b | 2014-10-08 01:12:15 -0300 | [diff] [blame] | 120 | .description = "test array sort"); |
| 121 | |
| 122 | static int test_array_remove_at(const struct test *t) |
| 123 | { |
| 124 | struct array array; |
| 125 | const char *c1 = "test1"; |
| 126 | const char *c2 = "test2"; |
| 127 | const char *c3 = "test3"; |
| 128 | |
| 129 | array_init(&array, 2); |
| 130 | array_append(&array, c1); |
| 131 | array_append(&array, c2); |
| 132 | array_append(&array, c3); |
| 133 | |
| 134 | array_remove_at(&array, 2); |
| 135 | assert_return(array.count == 2, EXIT_FAILURE); |
| 136 | assert_return(array.array[0] == c1, EXIT_FAILURE); |
| 137 | assert_return(array.array[1] == c2, EXIT_FAILURE); |
| 138 | |
| 139 | array_remove_at(&array, 0); |
| 140 | assert_return(array.count == 1, EXIT_FAILURE); |
| 141 | assert_return(array.array[0] == c2, EXIT_FAILURE); |
| 142 | |
| 143 | array_remove_at(&array, 0); |
| 144 | assert_return(array.count == 0, EXIT_FAILURE); |
| 145 | |
| 146 | array_append(&array, c1); |
| 147 | array_append(&array, c2); |
| 148 | array_append(&array, c3); |
| 149 | |
| 150 | array_remove_at(&array, 1); |
| 151 | assert_return(array.count == 2, EXIT_FAILURE); |
| 152 | assert_return(array.array[0] == c1, EXIT_FAILURE); |
| 153 | assert_return(array.array[1] == c3, EXIT_FAILURE); |
| 154 | |
| 155 | array_free_array(&array); |
| 156 | |
| 157 | return 0; |
| 158 | } |
Lucas De Marchi | f1155c1 | 2014-10-09 13:00:30 -0300 | [diff] [blame] | 159 | DEFINE_TEST(test_array_remove_at, |
Lucas De Marchi | fdafa6b | 2014-10-08 01:12:15 -0300 | [diff] [blame] | 160 | .description = "test array remove at"); |
| 161 | |
Lucas De Marchi | c78066a | 2015-01-08 17:10:18 -0200 | [diff] [blame] | 162 | static int test_array_pop(const struct test *t) |
| 163 | { |
| 164 | struct array array; |
| 165 | const char *c1 = "test1"; |
| 166 | const char *c2 = "test2"; |
| 167 | const char *c3 = "test3"; |
| 168 | |
| 169 | array_init(&array, 2); |
| 170 | array_append(&array, c1); |
| 171 | array_append(&array, c2); |
| 172 | array_append(&array, c3); |
| 173 | |
| 174 | |
| 175 | array_pop(&array); |
| 176 | |
| 177 | assert_return(array.count == 2, EXIT_FAILURE); |
| 178 | assert_return(array.array[0] == c1, EXIT_FAILURE); |
| 179 | assert_return(array.array[1] == c2, EXIT_FAILURE); |
| 180 | |
| 181 | array_pop(&array); |
| 182 | array_pop(&array); |
| 183 | |
| 184 | assert_return(array.count == 0, EXIT_FAILURE); |
| 185 | |
| 186 | array_free_array(&array); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | DEFINE_TEST(test_array_pop, |
| 192 | .description = "test array pop"); |
| 193 | |
Lucas De Marchi | 4328982 | 2014-10-09 14:29:04 -0300 | [diff] [blame] | 194 | TESTSUITE_MAIN(); |