|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Intel Corporation |
| 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 are met: |
| 7 | + * * Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * * Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * * Neither the name of the Intel Corporation nor the |
| 13 | + * names of its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * |
| 28 | + * Author: Michal Jerzy Wierzbicki <michalx.wierzbicki@linux.intel.com> |
| 29 | + */ |
| 30 | + |
| 31 | +#include <sof/alloc.h> |
| 32 | + |
| 33 | +#include <stdarg.h> |
| 34 | +#include <setjmp.h> |
| 35 | +#include <cmocka.h> |
| 36 | + |
| 37 | +static void test_lib_lib_rstrcmp_for_a_and_a_equals_0(void **state) |
| 38 | +{ |
| 39 | + int r; |
| 40 | + const char *str1 = "a"; |
| 41 | + const char *str2 = "a"; |
| 42 | + |
| 43 | + (void) state; |
| 44 | + |
| 45 | + r = rstrcmp(str1, str2); |
| 46 | + assert_int_equal(r, 0); |
| 47 | +} |
| 48 | + |
| 49 | +static void test_lib_lib_rstrcmp_for_a_and_b_is_negative(void **state) |
| 50 | +{ |
| 51 | + int r; |
| 52 | + const char *str1 = "a"; |
| 53 | + const char *str2 = "b"; |
| 54 | + |
| 55 | + (void) state; |
| 56 | + |
| 57 | + r = rstrcmp(str1, str2); |
| 58 | + assert_true(r < 0); |
| 59 | +} |
| 60 | + |
| 61 | +static void test_lib_lib_rstrcmp_for_b_and_a_is_positive(void **state) |
| 62 | +{ |
| 63 | + int r; |
| 64 | + const char *str1 = "b"; |
| 65 | + const char *str2 = "a"; |
| 66 | + |
| 67 | + (void) state; |
| 68 | + |
| 69 | + r = rstrcmp(str1, str2); |
| 70 | + assert_true(r > 0); |
| 71 | +} |
| 72 | + |
| 73 | +static void test_lib_lib_rstrcmp_for_empty_and_null_str_equals_0(void **state) |
| 74 | +{ |
| 75 | + int r; |
| 76 | + const char *str1 = ""; |
| 77 | + const char *str2 = "\0"; |
| 78 | + |
| 79 | + (void) state; |
| 80 | + |
| 81 | + r = rstrcmp(str1, str2); |
| 82 | + assert_int_equal(r, 0); |
| 83 | +} |
| 84 | + |
| 85 | +static void test_lib_lib_rstrcmp_for_abc_and_abcd_is_negative(void **state) |
| 86 | +{ |
| 87 | + int r; |
| 88 | + const char *str1 = "abc"; |
| 89 | + const char *str2 = "abcd"; |
| 90 | + |
| 91 | + (void) state; |
| 92 | + |
| 93 | + r = rstrcmp(str1, str2); |
| 94 | + assert_true(r < 0); |
| 95 | +} |
| 96 | + |
| 97 | +static void test_lib_lib_rstrcmp_for_abcd_and_abc_is_positive(void **state) |
| 98 | +{ |
| 99 | + int r; |
| 100 | + const char *str1 = "abcd"; |
| 101 | + const char *str2 = "abc"; |
| 102 | + |
| 103 | + (void) state; |
| 104 | + |
| 105 | + r = rstrcmp(str1, str2); |
| 106 | + |
| 107 | + assert_true(r > 0); |
| 108 | +} |
| 109 | + |
| 110 | +static void test_lib_lib_rstrcmp_for_abc_and_aBc_is_positive(void **state) |
| 111 | +{ |
| 112 | + int r; |
| 113 | + const char *str1 = "abc"; |
| 114 | + const char *str2 = "aBc"; |
| 115 | + |
| 116 | + (void) state; |
| 117 | + |
| 118 | + r = rstrcmp(str1, str2); |
| 119 | + assert_true(r > 0); |
| 120 | +} |
| 121 | + |
| 122 | +static void test_lib_lib_rstrcmp_for_same_multinull_equals_0(void **state) |
| 123 | +{ |
| 124 | + int r; |
| 125 | + const char *str1 = "Lorem\0Ipsum\0"; |
| 126 | + const char *str2 = "Lorem\0Ipsum\0"; |
| 127 | + |
| 128 | + (void) state; |
| 129 | + |
| 130 | + r = rstrcmp(str1, str2); |
| 131 | + assert_int_equal(r, 0); |
| 132 | +} |
| 133 | + |
| 134 | +static void test_lib_lib_rstrcmp_for_diff_after_null_equals_0(void **state) |
| 135 | +{ |
| 136 | + int r; |
| 137 | + const char *str1 = "Lorem\0Ipsum\0"; |
| 138 | + const char *str2 = "Lorem\0IPzum\0"; |
| 139 | + |
| 140 | + (void) state; |
| 141 | + |
| 142 | + r = rstrcmp(str1, str2); |
| 143 | + assert_int_equal(r, 0); |
| 144 | +} |
| 145 | + |
| 146 | +static void test_lib_lib_rstrcmp_for_verylongstrings_equals_0(void **state) |
| 147 | +{ |
| 148 | + int r; |
| 149 | + int i; |
| 150 | + const int size = 2048; |
| 151 | + char str1[size+1]; |
| 152 | + char str2[size+1]; |
| 153 | + |
| 154 | + for (i = 0; i < size; ++i) { |
| 155 | + str1[i] = 'a'; |
| 156 | + str2[i] = 'a'; |
| 157 | + } |
| 158 | + str1[size] = 0; |
| 159 | + str2[size] = 0; |
| 160 | + |
| 161 | + (void) state; |
| 162 | + |
| 163 | + r = rstrcmp(str1, str2); |
| 164 | + assert_int_equal(r, 0); |
| 165 | +} |
| 166 | + |
| 167 | +static void test_lib_lib_rstrcmp_for_verylongstrings_is_positive(void **state) |
| 168 | +{ |
| 169 | + int r; |
| 170 | + int i; |
| 171 | + const int size = 2048; |
| 172 | + char str1[size+1]; |
| 173 | + char str2[size+1]; |
| 174 | + |
| 175 | + for (i = 0; i < size-1; ++i) { |
| 176 | + str1[i] = 'a'; |
| 177 | + str2[i] = 'a'; |
| 178 | + } |
| 179 | + str1[size-1] = 'a'; |
| 180 | + str2[size-1] = 'A'; |
| 181 | + str1[size] = 0; |
| 182 | + str2[size] = 0; |
| 183 | + |
| 184 | + (void) state; |
| 185 | + |
| 186 | + r = rstrcmp(str1, str2); |
| 187 | + assert_true(r > 0); |
| 188 | +} |
| 189 | + |
| 190 | +static void test_lib_lib_rstrcmp_for_verylongstrings_is_negative(void **state) |
| 191 | +{ |
| 192 | + int r; |
| 193 | + int i; |
| 194 | + const int size = 2048; |
| 195 | + char str1[size+1]; |
| 196 | + char str2[size+1]; |
| 197 | + |
| 198 | + for (i = 0; i < size-1; ++i) { |
| 199 | + str1[i] = 'a'; |
| 200 | + str2[i] = 'a'; |
| 201 | + } |
| 202 | + str1[size-1] = 'A'; |
| 203 | + str2[size-1] = 'a'; |
| 204 | + str1[size] = 0; |
| 205 | + str2[size] = 0; |
| 206 | + |
| 207 | + (void) state; |
| 208 | + |
| 209 | + r = rstrcmp(str1, str2); |
| 210 | + assert_true(r < 0); |
| 211 | +} |
| 212 | + |
| 213 | +int main(void) |
| 214 | +{ |
| 215 | + const struct CMUnitTest tests[] = { |
| 216 | + cmocka_unit_test( |
| 217 | + test_lib_lib_rstrcmp_for_a_and_a_equals_0), |
| 218 | + cmocka_unit_test( |
| 219 | + test_lib_lib_rstrcmp_for_a_and_b_is_negative), |
| 220 | + cmocka_unit_test( |
| 221 | + test_lib_lib_rstrcmp_for_b_and_a_is_positive), |
| 222 | + cmocka_unit_test( |
| 223 | + test_lib_lib_rstrcmp_for_empty_and_null_str_equals_0), |
| 224 | + cmocka_unit_test( |
| 225 | + test_lib_lib_rstrcmp_for_abc_and_abcd_is_negative), |
| 226 | + cmocka_unit_test( |
| 227 | + test_lib_lib_rstrcmp_for_abcd_and_abc_is_positive), |
| 228 | + cmocka_unit_test( |
| 229 | + test_lib_lib_rstrcmp_for_abc_and_aBc_is_positive), |
| 230 | + cmocka_unit_test( |
| 231 | + test_lib_lib_rstrcmp_for_same_multinull_equals_0), |
| 232 | + cmocka_unit_test( |
| 233 | + test_lib_lib_rstrcmp_for_diff_after_null_equals_0), |
| 234 | + cmocka_unit_test( |
| 235 | + test_lib_lib_rstrcmp_for_verylongstrings_equals_0), |
| 236 | + cmocka_unit_test( |
| 237 | + test_lib_lib_rstrcmp_for_verylongstrings_is_positive), |
| 238 | + cmocka_unit_test( |
| 239 | + test_lib_lib_rstrcmp_for_verylongstrings_is_negative), |
| 240 | + }; |
| 241 | + |
| 242 | + cmocka_set_message_output(CM_OUTPUT_TAP); |
| 243 | + |
| 244 | + return cmocka_run_group_tests(tests, NULL, NULL); |
| 245 | +} |
0 commit comments