| 1 |
/* |
| 2 |
* Copyright © 2014 Red Hat, Inc. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice (including the next |
| 12 |
* paragraph) shall be included in all copies or substantial portions of the |
| 13 |
* Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <config.h> |
| 25 |
|
| 26 |
#include <errno.h> |
| 27 |
#include <fcntl.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <valgrind/valgrind.h> |
| 30 |
|
| 31 |
#include "util-bits.h" |
| 32 |
#include "util-files.h" |
| 33 |
#include "util-input-event.h" |
| 34 |
#include "util-list.h" |
| 35 |
#include "util-macros.h" |
| 36 |
#include "util-matrix.h" |
| 37 |
#include "util-mem.h" |
| 38 |
#include "util-newtype.h" |
| 39 |
#include "util-prop-parsers.h" |
| 40 |
#include "util-range.h" |
| 41 |
#include "util-ratelimit.h" |
| 42 |
#include "util-stringbuf.h" |
| 43 |
#include "util-strings.h" |
| 44 |
#include "util-time.h" |
| 45 |
|
| 46 |
#include "evdev-frame.h" |
| 47 |
#include "litest-runner.h" |
| 48 |
#include "litest.h" |
| 49 |
|
| 50 |
#define TEST_VERSIONSORT |
| 51 |
#include "libinput-versionsort.h" |
| 52 |
|
| 53 |
START_TEST(auto_test) |
| 54 |
{ |
| 55 |
/* This one is just a compile test */ |
| 56 |
auto tv = usec_to_timeval(usec_from_uint64_t(0)); |
| 57 |
tv.tv_sec = 0; |
| 58 |
litest_assert_int_eq(tv.tv_sec, 0); |
| 59 |
} |
| 60 |
END_TEST |
| 61 |
|
| 62 |
START_TEST(mkdir_p_test) |
| 63 |
{ |
| 64 |
const char *testdir = "/tmp/litest_mkdir_test"; |
| 65 |
litest_assert_neg_errno_success(mkdir_p("/")); |
| 66 |
|
| 67 |
rmdir(testdir); |
| 68 |
litest_assert_neg_errno_success(mkdir_p(testdir)); |
| 69 |
/* EEXIST is not an error */ |
| 70 |
litest_assert_neg_errno_success(mkdir_p(testdir)); |
| 71 |
rmdir(testdir); |
| 72 |
|
| 73 |
int ret = mkdir_p("/proc/foo"); |
| 74 |
litest_assert_msg(ret == -ENOENT || ret == -EACCES, |
| 75 |
"mkdir_p(\"/proc/foo\") returned %d\n", |
| 76 |
ret); |
| 77 |
} |
| 78 |
END_TEST |
| 79 |
|
| 80 |
START_TEST(rmdir_r_test) |
| 81 |
{ |
| 82 |
const char *testdir = "/tmp/litest_rmdir_test"; |
| 83 |
_autofree_ char *path = strdup_printf("%s/foo/bar/baz", testdir); |
| 84 |
mkdir_p(path); |
| 85 |
|
| 86 |
_autofree_ char *f1 = strdup_printf("%s/remain", testdir); |
| 87 |
_autofree_ char *f2 = strdup_printf("%s/foo/remove", testdir); |
| 88 |
_autofree_ char *f3 = strdup_printf("%s/foo/bar/to-remove", testdir); |
| 89 |
_autofree_ char *f4 = strdup_printf("%s/foo/bar/baz/wipeme", testdir); |
| 90 |
|
| 91 |
litest_assert_errno_success(close(open(f1, O_WRONLY | O_CREAT, 0644))); |
| 92 |
litest_assert_errno_success(close(open(f2, O_WRONLY | O_CREAT, 0644))); |
| 93 |
litest_assert_errno_success(close(open(f3, O_WRONLY | O_CREAT, 0644))); |
| 94 |
litest_assert_errno_success(close(open(f4, O_WRONLY | O_CREAT, 0644))); |
| 95 |
|
| 96 |
struct stat st; |
| 97 |
litest_assert_errno_success(stat(f1, &st)); |
| 98 |
litest_assert_errno_success(stat(f2, &st)); |
| 99 |
litest_assert_errno_success(stat(f3, &st)); |
| 100 |
litest_assert_errno_success(stat(f4, &st)); |
| 101 |
|
| 102 |
_autofree_ char *rmpath = strdup_printf("%s/foo/", testdir); |
| 103 |
int rc = rmdir_r(rmpath); |
| 104 |
litest_assert_neg_errno_success(rc); |
| 105 |
|
| 106 |
litest_assert_errno_success(stat(f1, &st)); |
| 107 |
litest_assert_errno_success(stat(testdir, &st)); |
| 108 |
|
| 109 |
rc = stat(f2, &st) < 0 ? -errno : 0; |
| 110 |
litest_assert_int_eq(rc, -ENOENT); |
| 111 |
rc = stat(f3, &st) < 0 ? -errno : 0; |
| 112 |
litest_assert_int_eq(rc, -ENOENT); |
| 113 |
rc = stat(f4, &st) < 0 ? -errno : 0; |
| 114 |
litest_assert_int_eq(rc, -ENOENT); |
| 115 |
} |
| 116 |
END_TEST |
| 117 |
|
| 118 |
START_TEST(tmpdir_test) |
| 119 |
{ |
| 120 |
_autofree_ char *tmpdir_path = NULL; |
| 121 |
{ |
| 122 |
_destroy_(tmpdir) *tmpdir = tmpdir_create(NULL); |
| 123 |
|
| 124 |
tmpdir_path = safe_strdup(tmpdir->path); |
| 125 |
|
| 126 |
_autofree_ char *f1 = strdup_printf("%s/wipeme", tmpdir_path); |
| 127 |
litest_assert_errno_success(close(open(f1, O_WRONLY | O_CREAT, 0644))); |
| 128 |
} |
| 129 |
struct stat st; |
| 130 |
int rc = stat(tmpdir_path, &st) < 0 ? -errno : 0; |
| 131 |
litest_assert_int_eq(rc, -ENOENT); |
| 132 |
} |
| 133 |
END_TEST |
| 134 |
|
| 135 |
START_TEST(find_files_test) |
| 136 |
{ |
| 137 |
_autofree_ char *dirname = strdup("/tmp/litest_find_files_test.XXXXXX"); |
| 138 |
mkdtemp(dirname); |
| 139 |
|
| 140 |
_autofree_ char *d1 = strdup_printf("%s/d1", dirname); |
| 141 |
_autofree_ char *d2 = strdup_printf("%s/d2", dirname); |
| 142 |
_autofree_ char *d3 = strdup_printf("%s/d3", dirname); |
| 143 |
|
| 144 |
litest_assert_neg_errno_success(mkdir_p(d1)); |
| 145 |
litest_assert_neg_errno_success(mkdir_p(d2)); |
| 146 |
litest_assert_neg_errno_success(mkdir_p(d3)); |
| 147 |
|
| 148 |
/* clang-format off */ |
| 149 |
struct f { |
| 150 |
const char *name; |
| 151 |
const char *dir1; |
| 152 |
const char *dir2; |
| 153 |
const char *dir3; |
| 154 |
char *expected; |
| 155 |
} files[] = { |
| 156 |
{ "10-abc.suf", d1, d2, d3 }, |
| 157 |
{ "20-def.suf", d1, NULL, d3 }, |
| 158 |
{ "30-ghi.suf", d1, d2, NULL }, |
| 159 |
{ "40-jkl.suf", NULL, d2, NULL }, |
| 160 |
{ "50-mno.suf", NULL, d2, d3 }, |
| 161 |
{ "60-pgr.suf", NULL, NULL, d3 }, |
| 162 |
{ "70-abc.suf", NULL, NULL, d3 }, |
| 163 |
{ "21-xyz.fix", NULL, NULL, d3 }, |
| 164 |
{ "35-uvw.fix", NULL, d2, d3 }, |
| 165 |
{ "70-rst.fix", d1, NULL, d3 }, |
| 166 |
{ NULL }, |
| 167 |
}; |
| 168 |
/* clang-format on */ |
| 169 |
for (struct f *f = files; f->name; f++) { |
| 170 |
if (f->dir1) { |
| 171 |
_autofree_ char *path = |
| 172 |
strdup_printf("%s/%s", f->dir1, f->name); |
| 173 |
close(open(path, O_WRONLY | O_CREAT, 0644)); |
| 174 |
f->expected = steal(&path); |
| 175 |
} |
| 176 |
if (f->dir2) { |
| 177 |
_autofree_ char *path = |
| 178 |
strdup_printf("%s/%s", f->dir2, f->name); |
| 179 |
close(open(path, O_WRONLY | O_CREAT, 0644)); |
| 180 |
if (!f->expected) |
| 181 |
f->expected = steal(&path); |
| 182 |
} |
| 183 |
if (f->dir3) { |
| 184 |
_autofree_ char *path = |
| 185 |
strdup_printf("%s/%s", f->dir3, f->name); |
| 186 |
close(open(path, O_WRONLY | O_CREAT, 0644)); |
| 187 |
if (!f->expected) |
| 188 |
f->expected = steal(&path); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
const char *dirs[] = { d1, d2, d3, NULL }; |
| 193 |
size_t nfiles; |
| 194 |
_autostrvfree_ char **paths = list_files(dirs, "suf", &nfiles); |
| 195 |
litest_assert_int_eq(nfiles, (size_t)7); |
| 196 |
litest_assert_str_eq(paths[0], files[0].expected); |
| 197 |
litest_assert_str_eq(paths[1], files[1].expected); |
| 198 |
litest_assert_str_eq(paths[2], files[2].expected); |
| 199 |
litest_assert_str_eq(paths[3], files[3].expected); |
| 200 |
litest_assert_str_eq(paths[4], files[4].expected); |
| 201 |
litest_assert_str_eq(paths[5], files[5].expected); |
| 202 |
litest_assert_str_eq(paths[6], files[6].expected); |
| 203 |
litest_assert_str_eq(paths[7], NULL); |
| 204 |
|
| 205 |
for (struct f *f = files; f->name; f++) { |
| 206 |
if (f->dir1) { |
| 207 |
_autofree_ char *path = |
| 208 |
strdup_printf("%s/%s", f->dir1, f->name); |
| 209 |
unlink(path); |
| 210 |
} |
| 211 |
if (f->dir2) { |
| 212 |
_autofree_ char *path = |
| 213 |
strdup_printf("%s/%s", f->dir2, f->name); |
| 214 |
unlink(path); |
| 215 |
} |
| 216 |
if (f->dir3) { |
| 217 |
_autofree_ char *path = |
| 218 |
strdup_printf("%s/%s", f->dir3, f->name); |
| 219 |
unlink(path); |
| 220 |
} |
| 221 |
free(f->expected); |
| 222 |
} |
| 223 |
rmdir(d1); |
| 224 |
rmdir(d2); |
| 225 |
rmdir(d3); |
| 226 |
rmdir(dirname); |
| 227 |
|
| 228 |
const char *empty[] = { NULL }; |
| 229 |
_autostrvfree_ char **empty_path = list_files(empty, "suf", &nfiles); |
| 230 |
litest_assert_int_eq(nfiles, (size_t)0); |
| 231 |
litest_assert_ptr_notnull(empty_path); |
| 232 |
litest_assert_ptr_null(empty_path[0]); |
| 233 |
|
| 234 |
_autostrvfree_ char **also_empty_path = list_files(NULL, "suf", &nfiles); |
| 235 |
litest_assert_int_eq(nfiles, (size_t)0); |
| 236 |
litest_assert_ptr_notnull(also_empty_path); |
| 237 |
litest_assert_ptr_null(also_empty_path[0]); |
| 238 |
} |
| 239 |
END_TEST |
| 240 |
|
| 241 |
START_TEST(array_for_each) |
| 242 |
{ |
| 243 |
int ai[6]; |
| 244 |
char ac[10]; |
| 245 |
struct as { |
| 246 |
int a; |
| 247 |
char b; |
| 248 |
int *ptr; |
| 249 |
} as[32]; |
| 250 |
|
| 251 |
for (size_t i = 0; i < 6; i++) |
| 252 |
ai[i] = 20 + i; |
| 253 |
for (size_t i = 0; i < 10; i++) |
| 254 |
ac[i] = 100 + i; |
| 255 |
for (size_t i = 0; i < 32; i++) { |
| 256 |
as[i].a = 10 + i; |
| 257 |
as[i].b = 20 + i; |
| 258 |
as[i].ptr = (int *)0xab + i; |
| 259 |
} |
| 260 |
|
| 261 |
int iexpected = 20; |
| 262 |
ARRAY_FOR_EACH(ai, entry) { |
| 263 |
litest_assert_int_eq(*entry, iexpected); |
| 264 |
++iexpected; |
| 265 |
} |
| 266 |
litest_assert_int_eq(iexpected, 26); |
| 267 |
|
| 268 |
int cexpected = 100; |
| 269 |
ARRAY_FOR_EACH(ac, entry) { |
| 270 |
litest_assert_int_eq(*entry, cexpected); |
| 271 |
++cexpected; |
| 272 |
} |
| 273 |
litest_assert_int_eq(cexpected, 110); |
| 274 |
|
| 275 |
struct as sexpected = { |
| 276 |
.a = 10, |
| 277 |
.b = 20, |
| 278 |
.ptr = (int *)0xab, |
| 279 |
}; |
| 280 |
ARRAY_FOR_EACH(as, entry) { |
| 281 |
litest_assert_int_eq(entry->a, sexpected.a); |
| 282 |
litest_assert_int_eq(entry->b, sexpected.b); |
| 283 |
litest_assert_ptr_eq(entry->ptr, sexpected.ptr); |
| 284 |
++sexpected.a; |
| 285 |
++sexpected.b; |
| 286 |
++sexpected.ptr; |
| 287 |
} |
| 288 |
litest_assert_int_eq(sexpected.a, 42); |
| 289 |
} |
| 290 |
END_TEST |
| 291 |
|
| 292 |
START_TEST(bitfield_helpers) |
| 293 |
{ |
| 294 |
/* This value has a bit set on all of the word boundaries we want to |
| 295 |
* test: 0, 1, 7, 8, 31, 32, and 33 |
| 296 |
*/ |
| 297 |
unsigned char read_bitfield[] = { 0x83, 0x1, 0x0, 0x80, 0x3 }; |
| 298 |
unsigned char write_bitfield[ARRAY_LENGTH(read_bitfield)] = { 0 }; |
| 299 |
size_t i; |
| 300 |
|
| 301 |
/* Now check that the bitfield we wrote to came out to be the same as |
| 302 |
* the bitfield we were writing from */ |
| 303 |
for (i = 0; i < ARRAY_LENGTH(read_bitfield) * 8; i++) { |
| 304 |
switch (i) { |
| 305 |
case 0: |
| 306 |
case 1: |
| 307 |
case 7: |
| 308 |
case 8: |
| 309 |
case 31: |
| 310 |
case 32: |
| 311 |
case 33: |
| 312 |
litest_assert(bit_is_set(read_bitfield, i)); |
| 313 |
set_bit(write_bitfield, i); |
| 314 |
break; |
| 315 |
default: |
| 316 |
litest_assert(!bit_is_set(read_bitfield, i)); |
| 317 |
clear_bit(write_bitfield, i); |
| 318 |
break; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
litest_assert_int_eq( |
| 323 |
memcmp(read_bitfield, write_bitfield, sizeof(read_bitfield)), |
| 324 |
0); |
| 325 |
} |
| 326 |
END_TEST |
| 327 |
|
| 328 |
START_TEST(bitmask_test) |
| 329 |
{ |
| 330 |
{ |
| 331 |
bitmask_t mask1 = bitmask_from_u32(0x12345678U); |
| 332 |
litest_assert(bitmask_as_u32(mask1) == 0x12345678U); |
| 333 |
|
| 334 |
bitmask_t mask2 = bitmask_from_u32(0); |
| 335 |
litest_assert_int_eq(bitmask_as_u32(mask2), 0U); |
| 336 |
|
| 337 |
bitmask_t mask3 = bitmask_from_u32(0xFFFFFFFFU); |
| 338 |
litest_assert_int_eq(bitmask_as_u32(mask3), 0xFFFFFFFFU); |
| 339 |
} |
| 340 |
{ |
| 341 |
bitmask_t mask1 = bitmask_new(); |
| 342 |
litest_assert(bitmask_is_empty(mask1)); |
| 343 |
|
| 344 |
bitmask_t mask2 = bitmask_from_u32(0x00000001U); |
| 345 |
litest_assert(!bitmask_is_empty(mask2)); |
| 346 |
|
| 347 |
bitmask_t mask3 = bitmask_from_u32(0xFFFFFFFFU); |
| 348 |
litest_assert(!bitmask_is_empty(mask3)); |
| 349 |
} |
| 350 |
{ |
| 351 |
bitmask_t mask1 = bitmask_from_u32(0x0000000FU); |
| 352 |
bitmask_t bits1 = bitmask_from_u32(0x00000003U); |
| 353 |
litest_assert(bitmask_any(mask1, bits1)); |
| 354 |
|
| 355 |
bitmask_t mask2 = bitmask_from_u32(0x0000000FU); |
| 356 |
bitmask_t bits2 = bitmask_from_u32(0x000000F0U); |
| 357 |
litest_assert(!bitmask_any(mask2, bits2)); |
| 358 |
|
| 359 |
bitmask_t mask3 = bitmask_from_u32(0x00000000U); |
| 360 |
bitmask_t bits3 = bitmask_from_u32(0x00000001U); |
| 361 |
litest_assert(!bitmask_any(mask3, bits3)); |
| 362 |
|
| 363 |
bitmask_t mask4 = bitmask_from_u32(0xFFFFFFFFU); |
| 364 |
bitmask_t bits4 = bitmask_from_u32(0xFFFFFFFFU); |
| 365 |
litest_assert(bitmask_any(mask4, bits4)); |
| 366 |
|
| 367 |
bitmask_t mask5 = bitmask_from_u32(0x10000000U); |
| 368 |
bitmask_t bits5 = bitmask_from_u32(0x10000000U); |
| 369 |
litest_assert(bitmask_any(mask5, bits5)); |
| 370 |
} |
| 371 |
{ |
| 372 |
bitmask_t mask1 = bitmask_from_u32(0x0000000FU); |
| 373 |
bitmask_t bits1 = bitmask_from_u32(0x00000003U); |
| 374 |
litest_assert(bitmask_all(mask1, bits1)); |
| 375 |
litest_assert(!bitmask_all(bits1, mask1)); |
| 376 |
|
| 377 |
bitmask_t mask2 = bitmask_from_u32(0x0000000FU); |
| 378 |
bitmask_t bits2 = bitmask_from_u32(0x0000000FU); |
| 379 |
litest_assert(bitmask_all(mask2, bits2)); |
| 380 |
litest_assert(bitmask_all(bits2, mask2)); |
| 381 |
|
| 382 |
bitmask_t mask3 = bitmask_from_u32(0x00000000U); |
| 383 |
bitmask_t bits3 = bitmask_from_u32(0x00000000U); |
| 384 |
litest_assert(!bitmask_all(mask3, bits3)); /* zero is special */ |
| 385 |
|
| 386 |
bitmask_t mask4 = bitmask_from_u32(0xFFFFFFFFU); |
| 387 |
bitmask_t bits4 = bitmask_from_u32(0xFFFFFFFFU); |
| 388 |
litest_assert(bitmask_all(mask4, bits4)); |
| 389 |
|
| 390 |
bitmask_t mask5 = bitmask_from_u32(0x10000000U); |
| 391 |
bitmask_t bits5 = bitmask_from_u32(0x10000000U); |
| 392 |
litest_assert(bitmask_all(mask5, bits5)); |
| 393 |
} |
| 394 |
{ |
| 395 |
|
| 396 |
bitmask_t mask1 = bitmask_from_u32(0x0000000FU); |
| 397 |
bitmask_t bits1 = bitmask_from_u32(0x000000F0U); |
| 398 |
litest_assert(!bitmask_merge(&mask1, bits1)); |
| 399 |
litest_assert_int_eq(mask1.mask, 0x000000FFU); |
| 400 |
|
| 401 |
bitmask_t mask2 = bitmask_from_u32(0x0000000FU); |
| 402 |
bitmask_t bits2 = bitmask_from_u32(0x0000000FU); |
| 403 |
litest_assert(bitmask_merge(&mask2, bits2)); |
| 404 |
litest_assert_int_eq(mask2.mask, 0x0000000FU); |
| 405 |
|
| 406 |
bitmask_t mask3 = bitmask_new(); |
| 407 |
bitmask_t bits3 = bitmask_from_u32(0xFFFFFFFFU); |
| 408 |
litest_assert(!bitmask_merge(&mask3, bits3)); |
| 409 |
litest_assert_int_eq(mask3.mask, 0xFFFFFFFFU); |
| 410 |
|
| 411 |
bitmask_t mask4 = bitmask_from_u32(0x80000000U); |
| 412 |
bitmask_t bits4 = bitmask_from_u32(0x00000001U); |
| 413 |
litest_assert(!bitmask_merge(&mask4, bits4)); |
| 414 |
litest_assert_int_eq(mask4.mask, 0x80000001U); |
| 415 |
} |
| 416 |
{ |
| 417 |
bitmask_t mask1 = bitmask_from_u32(0x000000FFU); |
| 418 |
bitmask_t bits1 = bitmask_from_u32(0x0000000FU); |
| 419 |
litest_assert(bitmask_clear(&mask1, bits1)); |
| 420 |
litest_assert_int_eq(mask1.mask, 0x000000F0U); |
| 421 |
|
| 422 |
bitmask_t mask2 = bitmask_from_u32(0x0000000FU); |
| 423 |
bitmask_t bits2 = bitmask_from_u32(0x0000000FU); |
| 424 |
litest_assert(bitmask_clear(&mask2, bits2)); |
| 425 |
litest_assert_int_eq(mask2.mask, 0x00000000U); |
| 426 |
|
| 427 |
bitmask_t mask3 = bitmask_from_u32(0xFFFFFFFFU); |
| 428 |
bitmask_t bits3 = bitmask_from_u32(0x00000000U); |
| 429 |
litest_assert(!bitmask_clear(&mask3, bits3)); /* zero is special */ |
| 430 |
litest_assert_int_eq(mask3.mask, 0xFFFFFFFFU); |
| 431 |
|
| 432 |
bitmask_t mask4 = bitmask_from_u32(0xFFFFFFFFU); |
| 433 |
bitmask_t bits4 = bitmask_from_u32(0xFFFFFFFFU); |
| 434 |
litest_assert(bitmask_clear(&mask4, bits4)); |
| 435 |
litest_assert_int_eq(mask4.mask, 0x0U); |
| 436 |
} |
| 437 |
{ |
| 438 |
bitmask_t mask1 = bitmask_from_u32(0x00000001U); |
| 439 |
litest_assert(bitmask_bit_is_set(mask1, 0)); |
| 440 |
litest_assert(!bitmask_bit_is_set(mask1, 1)); |
| 441 |
|
| 442 |
bitmask_t mask2 = bitmask_from_u32(0x80000000U); |
| 443 |
litest_assert(bitmask_bit_is_set(mask2, 31)); |
| 444 |
litest_assert(!bitmask_bit_is_set(mask2, 0)); |
| 445 |
|
| 446 |
bitmask_t mask3 = bitmask_from_u32(0xFFFFFFFFU); |
| 447 |
litest_assert(bitmask_bit_is_set(mask3, 0)); |
| 448 |
litest_assert(bitmask_bit_is_set(mask3, 31)); |
| 449 |
litest_assert(bitmask_bit_is_set(mask3, 16)); |
| 450 |
|
| 451 |
bitmask_t mask4 = bitmask_new(); |
| 452 |
litest_assert(!bitmask_bit_is_set(mask4, 0)); |
| 453 |
litest_assert(!bitmask_bit_is_set(mask4, 1)); |
| 454 |
} |
| 455 |
{ |
| 456 |
bitmask_t mask1 = bitmask_new(); |
| 457 |
litest_assert(!bitmask_set_bit(&mask1, 0)); |
| 458 |
litest_assert_int_eq(mask1.mask, 0x00000001U); |
| 459 |
|
| 460 |
litest_assert(bitmask_set_bit(&mask1, 0)); |
| 461 |
litest_assert_int_eq(mask1.mask, 0x00000001U); |
| 462 |
|
| 463 |
litest_assert(!bitmask_set_bit(&mask1, 31)); |
| 464 |
litest_assert_int_eq(mask1.mask, 0x80000001U); |
| 465 |
|
| 466 |
bitmask_t mask2 = bitmask_from_u32(0x0000000FU); |
| 467 |
litest_assert(!bitmask_set_bit(&mask2, 4)); |
| 468 |
litest_assert_int_eq(mask2.mask, 0x0000001FU); |
| 469 |
litest_assert(bitmask_set_bit(&mask2, 4)); |
| 470 |
litest_assert_int_eq(mask2.mask, 0x0000001FU); |
| 471 |
} |
| 472 |
{ |
| 473 |
bitmask_t mask1 = bitmask_from_u32(0xFFFFFFFFU); |
| 474 |
litest_assert(bitmask_clear_bit(&mask1, 0)); |
| 475 |
litest_assert_int_eq(mask1.mask, 0xFFFFFFFEU); |
| 476 |
|
| 477 |
litest_assert(!bitmask_clear_bit(&mask1, 0)); |
| 478 |
litest_assert_int_eq(mask1.mask, 0xFFFFFFFEU); |
| 479 |
|
| 480 |
litest_assert(bitmask_clear_bit(&mask1, 31)); |
| 481 |
litest_assert_int_eq(mask1.mask, 0x7FFFFFFEU); |
| 482 |
|
| 483 |
bitmask_t mask2 = bitmask_from_u32(0x0000001FU); |
| 484 |
litest_assert(bitmask_clear_bit(&mask2, 4)); |
| 485 |
litest_assert_int_eq(mask2.mask, 0x0000000FU); |
| 486 |
litest_assert(!bitmask_clear_bit(&mask2, 4)); |
| 487 |
litest_assert_int_eq(mask2.mask, 0x0000000FU); |
| 488 |
} |
| 489 |
{ |
| 490 |
bitmask_t mask1 = bitmask_from_bit(0); |
| 491 |
litest_assert_int_eq(mask1.mask, 0x00000001U); |
| 492 |
|
| 493 |
bitmask_t mask2 = bitmask_from_bit(31); |
| 494 |
litest_assert_int_eq(mask2.mask, 0x80000000U); |
| 495 |
|
| 496 |
bitmask_t mask3 = bitmask_from_bit(16); |
| 497 |
litest_assert_int_eq(mask3.mask, 0x00010000U); |
| 498 |
} |
| 499 |
{ |
| 500 |
bitmask_t mask1 = bitmask_from_u32(0x12345678U); |
| 501 |
litest_assert_int_eq(mask1.mask, 0x12345678U); |
| 502 |
|
| 503 |
bitmask_t mask2 = bitmask_from_u32(0); |
| 504 |
litest_assert_int_eq(mask2.mask, 0U); |
| 505 |
|
| 506 |
bitmask_t mask3 = bitmask_from_u32(0xFFFFFFFFU); |
| 507 |
litest_assert_int_eq(mask3.mask, 0xFFFFFFFFU); |
| 508 |
} |
| 509 |
{ |
| 510 |
bitmask_t mask1 = bitmask_from_bits(1, 2, 5); |
| 511 |
litest_assert_int_eq(mask1.mask, bit(1) | bit(2) | bit(5)); |
| 512 |
|
| 513 |
bitmask_t mask2 = bitmask_from_bits(0); |
| 514 |
litest_assert_int_eq(mask2.mask, bit(0)); |
| 515 |
} |
| 516 |
{ |
| 517 |
bitmask_t mask1 = bitmask_from_masks(0x1, 0x2, 0x8); |
| 518 |
litest_assert_int_eq(mask1.mask, 0x0000000BU); |
| 519 |
|
| 520 |
bitmask_t mask2 = bitmask_from_masks(0x0); |
| 521 |
litest_assert_int_eq(mask2.mask, 0x00000000U); |
| 522 |
} |
| 523 |
} |
| 524 |
END_TEST |
| 525 |
|
| 526 |
START_TEST(matrix_helpers) |
| 527 |
{ |
| 528 |
struct matrix m1, m2, m3; |
| 529 |
float f[6] = { 1, 2, 3, 4, 5, 6 }; |
| 530 |
int x, y; |
| 531 |
int row, col; |
| 532 |
|
| 533 |
matrix_init_identity(&m1); |
| 534 |
|
| 535 |
for (row = 0; row < 3; row++) { |
| 536 |
for (col = 0; col < 3; col++) { |
| 537 |
litest_assert_int_eq(m1.val[row][col], (row == col) ? 1 : 0); |
| 538 |
} |
| 539 |
} |
| 540 |
litest_assert(matrix_is_identity(&m1)); |
| 541 |
|
| 542 |
matrix_from_farray6(&m2, f); |
| 543 |
litest_assert_int_eq(m2.val[0][0], 1); |
| 544 |
litest_assert_int_eq(m2.val[0][1], 2); |
| 545 |
litest_assert_int_eq(m2.val[0][2], 3); |
| 546 |
litest_assert_int_eq(m2.val[1][0], 4); |
| 547 |
litest_assert_int_eq(m2.val[1][1], 5); |
| 548 |
litest_assert_int_eq(m2.val[1][2], 6); |
| 549 |
litest_assert_int_eq(m2.val[2][0], 0); |
| 550 |
litest_assert_int_eq(m2.val[2][1], 0); |
| 551 |
litest_assert_int_eq(m2.val[2][2], 1); |
| 552 |
|
| 553 |
x = 100; |
| 554 |
y = 5; |
| 555 |
matrix_mult_vec(&m1, &x, &y); |
| 556 |
litest_assert_int_eq(x, 100); |
| 557 |
litest_assert_int_eq(y, 5); |
| 558 |
|
| 559 |
matrix_mult(&m3, &m1, &m1); |
| 560 |
litest_assert(matrix_is_identity(&m3)); |
| 561 |
|
| 562 |
matrix_init_scale(&m2, 2, 4); |
| 563 |
litest_assert_int_eq(m2.val[0][0], 2); |
| 564 |
litest_assert_int_eq(m2.val[0][1], 0); |
| 565 |
litest_assert_int_eq(m2.val[0][2], 0); |
| 566 |
litest_assert_int_eq(m2.val[1][0], 0); |
| 567 |
litest_assert_int_eq(m2.val[1][1], 4); |
| 568 |
litest_assert_int_eq(m2.val[1][2], 0); |
| 569 |
litest_assert_int_eq(m2.val[2][0], 0); |
| 570 |
litest_assert_int_eq(m2.val[2][1], 0); |
| 571 |
litest_assert_int_eq(m2.val[2][2], 1); |
| 572 |
|
| 573 |
matrix_mult_vec(&m2, &x, &y); |
| 574 |
litest_assert_int_eq(x, 200); |
| 575 |
litest_assert_int_eq(y, 20); |
| 576 |
|
| 577 |
matrix_init_translate(&m2, 10, 100); |
| 578 |
litest_assert_int_eq(m2.val[0][0], 1); |
| 579 |
litest_assert_int_eq(m2.val[0][1], 0); |
| 580 |
litest_assert_int_eq(m2.val[0][2], 10); |
| 581 |
litest_assert_int_eq(m2.val[1][0], 0); |
| 582 |
litest_assert_int_eq(m2.val[1][1], 1); |
| 583 |
litest_assert_int_eq(m2.val[1][2], 100); |
| 584 |
litest_assert_int_eq(m2.val[2][0], 0); |
| 585 |
litest_assert_int_eq(m2.val[2][1], 0); |
| 586 |
litest_assert_int_eq(m2.val[2][2], 1); |
| 587 |
|
| 588 |
matrix_mult_vec(&m2, &x, &y); |
| 589 |
litest_assert_int_eq(x, 210); |
| 590 |
litest_assert_int_eq(y, 120); |
| 591 |
|
| 592 |
matrix_to_farray6(&m2, f); |
| 593 |
litest_assert_int_eq(f[0], 1); |
| 594 |
litest_assert_int_eq(f[1], 0); |
| 595 |
litest_assert_int_eq(f[2], 10); |
| 596 |
litest_assert_int_eq(f[3], 0); |
| 597 |
litest_assert_int_eq(f[4], 1); |
| 598 |
litest_assert_int_eq(f[5], 100); |
| 599 |
} |
| 600 |
END_TEST |
| 601 |
|
| 602 |
START_TEST(ratelimit_helpers) |
| 603 |
{ |
| 604 |
struct ratelimit rl; |
| 605 |
unsigned int i, j; |
| 606 |
|
| 607 |
/* 10 attempts every 1000ms */ |
| 608 |
ratelimit_init(&rl, usec_from_millis(1000), 10); |
| 609 |
|
| 610 |
for (j = 0; j < 3; ++j) { |
| 611 |
/* a burst of 9 attempts must succeed */ |
| 612 |
for (i = 0; i < 9; ++i) { |
| 613 |
litest_assert_enum_eq(ratelimit_test(&rl), RATELIMIT_PASS); |
| 614 |
} |
| 615 |
|
| 616 |
/* the 10th attempt reaches the threshold */ |
| 617 |
litest_assert_enum_eq(ratelimit_test(&rl), RATELIMIT_THRESHOLD); |
| 618 |
|
| 619 |
/* ..then further attempts must fail.. */ |
| 620 |
litest_assert_enum_eq(ratelimit_test(&rl), RATELIMIT_EXCEEDED); |
| 621 |
|
| 622 |
/* ..regardless of how often we try. */ |
| 623 |
for (i = 0; i < 100; ++i) { |
| 624 |
litest_assert_enum_eq(ratelimit_test(&rl), RATELIMIT_EXCEEDED); |
| 625 |
} |
| 626 |
|
| 627 |
/* ..even after waiting 20ms */ |
| 628 |
msleep(100); |
| 629 |
for (i = 0; i < 100; ++i) { |
| 630 |
litest_assert_enum_eq(ratelimit_test(&rl), RATELIMIT_EXCEEDED); |
| 631 |
} |
| 632 |
|
| 633 |
/* but after 1000ms the counter is reset */ |
| 634 |
msleep(950); /* +50ms to account for time drifts */ |
| 635 |
} |
| 636 |
} |
| 637 |
END_TEST |
| 638 |
|
| 639 |
struct parser_test { |
| 640 |
char *tag; |
| 641 |
int expected_value; |
| 642 |
}; |
| 643 |
|
| 644 |
START_TEST(dpi_parser) |
| 645 |
{ |
| 646 |
/* clang-format off */ |
| 647 |
struct parser_test tests[] = { |
| 648 |
{ "450 *1800 3200", 1800 }, |
| 649 |
{ "*450 1800 3200", 450 }, |
| 650 |
{ "450 1800 *3200", 3200 }, |
| 651 |
{ "450 1800 3200", 3200 }, |
| 652 |
{ "450 1800 failboat", 0 }, |
| 653 |
{ "450 1800 *failboat", 0 }, |
| 654 |
{ "0 450 1800 *3200", 0 }, |
| 655 |
{ "450@37 1800@12 *3200@6", 3200 }, |
| 656 |
{ "450@125 1800@125 *3200@125 ", 3200 }, |
| 657 |
{ "450@125 *1800@125 3200@125", 1800 }, |
| 658 |
{ "*this @string fails", 0 }, |
| 659 |
{ "12@34 *45@", 0 }, |
| 660 |
{ "12@a *45@", 0 }, |
| 661 |
{ "12@a *45@25", 0 }, |
| 662 |
{ " * 12, 450, 800", 0 }, |
| 663 |
{ " *12, 450, 800", 12 }, |
| 664 |
{ "*12, *450, 800", 12 }, |
| 665 |
{ "*-23412, 450, 800", 0 }, |
| 666 |
{ "112@125, 450@125, 800@125, 900@-125", 0 }, |
| 667 |
{ "", 0 }, |
| 668 |
{ " ", 0 }, |
| 669 |
{ "* ", 0 }, |
| 670 |
{ NULL, 0 }, |
| 671 |
}; |
| 672 |
/* clang-format on */ |
| 673 |
int i, dpi; |
| 674 |
|
| 675 |
for (i = 0; tests[i].tag != NULL; i++) { |
| 676 |
dpi = parse_mouse_dpi_property(tests[i].tag); |
| 677 |
litest_assert_int_eq(dpi, tests[i].expected_value); |
| 678 |
} |
| 679 |
|
| 680 |
dpi = parse_mouse_dpi_property(NULL); |
| 681 |
litest_assert_int_eq(dpi, 0); |
| 682 |
} |
| 683 |
END_TEST |
| 684 |
|
| 685 |
START_TEST(wheel_click_parser) |
| 686 |
{ |
| 687 |
/* clang-format off */ |
| 688 |
struct parser_test tests[] = { |
| 689 |
{ "1", 1 }, |
| 690 |
{ "10", 10 }, |
| 691 |
{ "-12", -12 }, |
| 692 |
{ "360", 360 }, |
| 693 |
|
| 694 |
{ "0", 0 }, |
| 695 |
{ "-0", 0 }, |
| 696 |
{ "a", 0 }, |
| 697 |
{ "10a", 0 }, |
| 698 |
{ "10-", 0 }, |
| 699 |
{ "sadfasfd", 0 }, |
| 700 |
{ "361", 0 }, |
| 701 |
{ NULL, 0 }, |
| 702 |
}; |
| 703 |
/* clang-format on */ |
| 704 |
|
| 705 |
int i, angle; |
| 706 |
|
| 707 |
for (i = 0; tests[i].tag != NULL; i++) { |
| 708 |
angle = parse_mouse_wheel_click_angle_property(tests[i].tag); |
| 709 |
litest_assert_int_eq(angle, tests[i].expected_value); |
| 710 |
} |
| 711 |
} |
| 712 |
END_TEST |
| 713 |
|
| 714 |
START_TEST(wheel_click_count_parser) |
| 715 |
{ |
| 716 |
/* clang-format off */ |
| 717 |
struct parser_test tests[] = { |
| 718 |
{ "1", 1 }, |
| 719 |
{ "10", 10 }, |
| 720 |
{ "-12", -12 }, |
| 721 |
{ "360", 360 }, |
| 722 |
|
| 723 |
{ "0", 0 }, |
| 724 |
{ "-0", 0 }, |
| 725 |
{ "a", 0 }, |
| 726 |
{ "10a", 0 }, |
| 727 |
{ "10-", 0 }, |
| 728 |
{ "sadfasfd", 0 }, |
| 729 |
{ "361", 0 }, |
| 730 |
{ NULL, 0 } |
| 731 |
}; |
| 732 |
/* clang-format on */ |
| 733 |
|
| 734 |
int i, angle; |
| 735 |
|
| 736 |
for (i = 0; tests[i].tag != NULL; i++) { |
| 737 |
angle = parse_mouse_wheel_click_count_property(tests[i].tag); |
| 738 |
litest_assert_int_eq(angle, tests[i].expected_value); |
| 739 |
} |
| 740 |
|
| 741 |
angle = parse_mouse_wheel_click_count_property(NULL); |
| 742 |
litest_assert_int_eq(angle, 0); |
| 743 |
} |
| 744 |
END_TEST |
| 745 |
|
| 746 |
START_TEST(dimension_prop_parser) |
| 747 |
{ |
| 748 |
/* clang-format off */ |
| 749 |
struct parser_test_dimension { |
| 750 |
char *tag; |
| 751 |
bool success; |
| 752 |
size_t x, y; |
| 753 |
} tests[] = { |
| 754 |
{ "10x10", true, 10, 10 }, |
| 755 |
{ "1x20", true, 1, 20 }, |
| 756 |
{ "1x8000", true, 1, 8000 }, |
| 757 |
{ "238492x428210", true, 238492, 428210 }, |
| 758 |
{ "0x0", false, 0, 0 }, |
| 759 |
{ "-10x10", false, 0, 0 }, |
| 760 |
{ "-1", false, 0, 0 }, |
| 761 |
{ "1x-99", false, 0, 0 }, |
| 762 |
{ "0", false, 0, 0 }, |
| 763 |
{ "100", false, 0, 0 }, |
| 764 |
{ "", false, 0, 0 }, |
| 765 |
{ "abd", false, 0, 0 }, |
| 766 |
{ "xabd", false, 0, 0 }, |
| 767 |
{ "0xaf", false, 0, 0 }, |
| 768 |
{ "0x0x", false, 0, 0 }, |
| 769 |
{ "x10", false, 0, 0 }, |
| 770 |
{ NULL, false, 0, 0 }, |
| 771 |
}; |
| 772 |
/* clang-format on */ |
| 773 |
int i; |
| 774 |
size_t x, y; |
| 775 |
bool success; |
| 776 |
|
| 777 |
for (i = 0; tests[i].tag != NULL; i++) { |
| 778 |
x = y = 0xad; |
| 779 |
success = parse_dimension_property(tests[i].tag, &x, &y); |
| 780 |
litest_assert(success == tests[i].success); |
| 781 |
if (success) { |
| 782 |
litest_assert_int_eq(x, tests[i].x); |
| 783 |
litest_assert_int_eq(y, tests[i].y); |
| 784 |
} else { |
| 785 |
litest_assert_int_eq(x, 0xadU); |
| 786 |
litest_assert_int_eq(y, 0xadU); |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
success = parse_dimension_property(NULL, &x, &y); |
| 791 |
litest_assert(success == false); |
| 792 |
} |
| 793 |
END_TEST |
| 794 |
|
| 795 |
START_TEST(reliability_prop_parser) |
| 796 |
{ |
| 797 |
/* clang-format off */ |
| 798 |
struct parser_test_reliability { |
| 799 |
char *tag; |
| 800 |
bool success; |
| 801 |
enum switch_reliability reliability; |
| 802 |
} tests[] = { |
| 803 |
{ "reliable", true, RELIABILITY_RELIABLE }, |
| 804 |
{ "unreliable", true, RELIABILITY_UNRELIABLE }, |
| 805 |
{ "write_open", true, RELIABILITY_WRITE_OPEN }, |
| 806 |
{ "", false, 0 }, |
| 807 |
{ "0", false, 0 }, |
| 808 |
{ "1", false, 0 }, |
| 809 |
{ NULL, false, 0, }, |
| 810 |
}; |
| 811 |
/* clang-format on */ |
| 812 |
enum switch_reliability r; |
| 813 |
bool success; |
| 814 |
int i; |
| 815 |
|
| 816 |
for (i = 0; tests[i].tag != NULL; i++) { |
| 817 |
r = 0xaf; |
| 818 |
success = parse_switch_reliability_property(tests[i].tag, &r); |
| 819 |
litest_assert(success == tests[i].success); |
| 820 |
if (success) |
| 821 |
litest_assert_int_eq(r, tests[i].reliability); |
| 822 |
else |
| 823 |
litest_assert_int_eq(r, 0xafU); |
| 824 |
} |
| 825 |
|
| 826 |
success = parse_switch_reliability_property(NULL, &r); |
| 827 |
litest_assert(success == true); |
| 828 |
litest_assert_enum_eq(r, RELIABILITY_RELIABLE); |
| 829 |
|
| 830 |
success = parse_switch_reliability_property("foo", NULL); |
| 831 |
litest_assert(success == false); |
| 832 |
} |
| 833 |
END_TEST |
| 834 |
|
| 835 |
START_TEST(calibration_prop_parser) |
| 836 |
{ |
| 837 |
#define DEFAULT_VALUES { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 } |
| 838 |
const float untouched[6] = DEFAULT_VALUES; |
| 839 |
/* clang-format off */ |
| 840 |
struct parser_test_calibration { |
| 841 |
char *prop; |
| 842 |
bool success; |
| 843 |
float values[6]; |
| 844 |
} tests[] = { |
| 845 |
{ "", false, DEFAULT_VALUES }, |
| 846 |
{ "banana", false, DEFAULT_VALUES }, |
| 847 |
{ "1 2 3 a 5 6", false, DEFAULT_VALUES }, |
| 848 |
{ "2", false, DEFAULT_VALUES }, |
| 849 |
{ "2 3 4 5 6", false, DEFAULT_VALUES }, |
| 850 |
{ "1 2 3 4 5 6", true, DEFAULT_VALUES }, |
| 851 |
{ "6.00012 3.244 4.238 5.2421 6.0134 8.860", true, |
| 852 |
{ 6.00012, 3.244, 4.238, 5.2421, 6.0134, 8.860 }}, |
| 853 |
{ "0xff 2 3 4 5 6", false, DEFAULT_VALUES }, |
| 854 |
{ NULL, false, DEFAULT_VALUES }, |
| 855 |
}; |
| 856 |
/* clang-format on */ |
| 857 |
bool success; |
| 858 |
float calibration[6]; |
| 859 |
int rc; |
| 860 |
int i; |
| 861 |
|
| 862 |
for (i = 0; tests[i].prop != NULL; i++) { |
| 863 |
memcpy(calibration, untouched, sizeof(calibration)); |
| 864 |
|
| 865 |
success = parse_calibration_property(tests[i].prop, calibration); |
| 866 |
litest_assert_int_eq(success, tests[i].success); |
| 867 |
if (success) |
| 868 |
rc = memcmp(tests[i].values, calibration, sizeof(calibration)); |
| 869 |
else |
| 870 |
rc = memcmp(untouched, calibration, sizeof(calibration)); |
| 871 |
litest_assert_int_eq(rc, 0); |
| 872 |
} |
| 873 |
|
| 874 |
memcpy(calibration, untouched, sizeof(calibration)); |
| 875 |
|
| 876 |
success = parse_calibration_property(NULL, calibration); |
| 877 |
litest_assert(success == false); |
| 878 |
rc = memcmp(untouched, calibration, sizeof(calibration)); |
| 879 |
litest_assert_int_eq(rc, 0); |
| 880 |
} |
| 881 |
END_TEST |
| 882 |
|
| 883 |
START_TEST(range_prop_parser) |
| 884 |
{ |
| 885 |
/* clang-format off */ |
| 886 |
struct parser_test_range { |
| 887 |
char *tag; |
| 888 |
bool success; |
| 889 |
int hi, lo; |
| 890 |
} tests[] = { |
| 891 |
{ "10:8", true, 10, 8 }, |
| 892 |
{ "100:-1", true, 100, -1 }, |
| 893 |
{ "-203813:-502023", true, -203813, -502023 }, |
| 894 |
{ "238492:28210", true, 238492, 28210 }, |
| 895 |
{ "none", true, 0, 0 }, |
| 896 |
{ "0:0", false, 0, 0 }, |
| 897 |
{ "", false, 0, 0 }, |
| 898 |
{ "abcd", false, 0, 0 }, |
| 899 |
{ "10:30:10", false, 0, 0 }, |
| 900 |
{ NULL, false, 0, 0 }, |
| 901 |
}; |
| 902 |
/* clang-format on */ |
| 903 |
int i; |
| 904 |
int hi, lo; |
| 905 |
bool success; |
| 906 |
|
| 907 |
for (i = 0; tests[i].tag != NULL; i++) { |
| 908 |
hi = lo = 0xad; |
| 909 |
success = parse_range_property(tests[i].tag, &hi, &lo); |
| 910 |
litest_assert(success == tests[i].success); |
| 911 |
if (success) { |
| 912 |
litest_assert_int_eq(hi, tests[i].hi); |
| 913 |
litest_assert_int_eq(lo, tests[i].lo); |
| 914 |
} else { |
| 915 |
litest_assert_int_eq(hi, 0xad); |
| 916 |
litest_assert_int_eq(lo, 0xad); |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
success = parse_range_property(NULL, NULL, NULL); |
| 921 |
litest_assert(success == false); |
| 922 |
} |
| 923 |
END_TEST |
| 924 |
|
| 925 |
START_TEST(boolean_prop_parser) |
| 926 |
{ |
| 927 |
/* clang-format off */ |
| 928 |
struct parser_test_range { |
| 929 |
char *tag; |
| 930 |
bool success; |
| 931 |
bool b; |
| 932 |
} tests[] = { |
| 933 |
{ "0", true, false }, |
| 934 |
{ "1", true, true }, |
| 935 |
{ "-1", false, false }, |
| 936 |
{ "2", false, false }, |
| 937 |
{ "abcd", false, false }, |
| 938 |
{ NULL, false, false }, |
| 939 |
}; |
| 940 |
/* clang-format on */ |
| 941 |
int i; |
| 942 |
bool success, b; |
| 943 |
|
| 944 |
for (i = 0; tests[i].tag != NULL; i++) { |
| 945 |
b = false; |
| 946 |
success = parse_boolean_property(tests[i].tag, &b); |
| 947 |
litest_assert(success == tests[i].success); |
| 948 |
if (success) |
| 949 |
litest_assert_int_eq(b, tests[i].b); |
| 950 |
else |
| 951 |
litest_assert_int_eq(b, false); |
| 952 |
} |
| 953 |
|
| 954 |
success = parse_boolean_property(NULL, NULL); |
| 955 |
litest_assert(success == false); |
| 956 |
} |
| 957 |
END_TEST |
| 958 |
|
| 959 |
START_TEST(evcode_prop_parser) |
| 960 |
{ |
| 961 |
/* clang-format off */ |
| 962 |
struct parser_test_tuple { |
| 963 |
const char *prop; |
| 964 |
bool success; |
| 965 |
size_t nevents; |
| 966 |
struct input_event events[20]; |
| 967 |
} tests[] = { |
| 968 |
{ "+EV_KEY", true, 1, {{ .type = EV_KEY, .code = 0xffff, .value = 1 }} }, |
| 969 |
{ "-EV_ABS;", true, 1, {{ .type = EV_ABS, .code = 0xffff, .value = 0 }} }, |
| 970 |
{ "+ABS_X;", true, 1, {{ .type = EV_ABS, .code = ABS_X, .value = 1 }} }, |
| 971 |
{ "-SW_TABLET_MODE;", true, 1, {{ .type = EV_SW, .code = SW_TABLET_MODE, .value = 0 }} }, |
| 972 |
{ "+EV_SW", true, 1, {{ .type = EV_SW, .code = 0xffff, .value = 1 }} }, |
| 973 |
{ "-ABS_Y", true, 1, {{ .type = EV_ABS, .code = ABS_Y, .value = 0 }} }, |
| 974 |
{ "+EV_ABS:0x00", true, 1, {{ .type = EV_ABS, .code = ABS_X, .value = 1 }} }, |
| 975 |
{ "-EV_ABS:01", true, 1, {{ .type = EV_ABS, .code = ABS_Y, .value = 0 }} }, |
| 976 |
{ "+ABS_TILT_X;-ABS_TILT_Y;", true, 2, |
| 977 |
{{ .type = EV_ABS, .code = ABS_TILT_X, .value = 1 }, |
| 978 |
{ .type = EV_ABS, .code = ABS_TILT_Y, .value = 0}} }, |
| 979 |
{ "+BTN_TOOL_DOUBLETAP;+EV_KEY;-KEY_A", true, 3, |
| 980 |
{{ .type = EV_KEY, .code = BTN_TOOL_DOUBLETAP, .value = 1 } , |
| 981 |
{ .type = EV_KEY, .code = 0xffff, .value = 1 }, |
| 982 |
{ .type = EV_KEY, .code = KEY_A, .value = 0 }} }, |
| 983 |
{ "+REL_Y;-ABS_Z;+BTN_STYLUS", true, 3, |
| 984 |
{{ .type = EV_REL, .code = REL_Y, .value = 1}, |
| 985 |
{ .type = EV_ABS, .code = ABS_Z, .value = 0}, |
| 986 |
{ .type = EV_KEY, .code = BTN_STYLUS, .value = 1 }} }, |
| 987 |
{ "-REL_Y;+EV_KEY:0x123;-BTN_STYLUS", true, 3, |
| 988 |
{{ .type = EV_REL, .code = REL_Y, .value = 0 }, |
| 989 |
{ .type = EV_KEY, .code = 0x123, .value = 1 }, |
| 990 |
{ .type = EV_KEY, .code = BTN_STYLUS, .value = 0 }} }, |
| 991 |
{ .prop = "", .success = false }, |
| 992 |
{ .prop = "+", .success = false }, |
| 993 |
{ .prop = "-", .success = false }, |
| 994 |
{ .prop = "!", .success = false }, |
| 995 |
{ .prop = "+EV_FOO", .success = false }, |
| 996 |
{ .prop = "+EV_KEY;-EV_FOO", .success = false }, |
| 997 |
{ .prop = "+BTN_STYLUS;-EV_FOO", .success = false }, |
| 998 |
{ .prop = "-BTN_UNKNOWN", .success = false }, |
| 999 |
{ .prop = "+BTN_UNKNOWN;+EV_KEY", .success = false }, |
| 1000 |
{ .prop = "-PR_UNKNOWN", .success = false }, |
| 1001 |
{ .prop = "-BTN_STYLUS;+PR_UNKNOWN;-ABS_X", .success = false }, |
| 1002 |
{ .prop = "-EV_REL:0xffff", .success = false }, |
| 1003 |
{ .prop = "-EV_REL:0x123.", .success = false }, |
| 1004 |
{ .prop = "-EV_REL:ffff", .success = false }, |
| 1005 |
{ .prop = "-EV_REL:blah", .success = false }, |
| 1006 |
{ .prop = "+KEY_A:0x11", .success = false }, |
| 1007 |
{ .prop = "+EV_KEY:0x11 ", .success = false }, |
| 1008 |
{ .prop = "+EV_KEY:0x11not", .success = false }, |
| 1009 |
{ .prop = "none", .success = false }, |
| 1010 |
{ .prop = NULL }, |
| 1011 |
}; |
| 1012 |
/* clang-format on */ |
| 1013 |
struct parser_test_tuple *t; |
| 1014 |
|
| 1015 |
for (int i = 0; tests[i].prop; i++) { |
| 1016 |
bool success; |
| 1017 |
struct input_event events[32]; |
| 1018 |
size_t nevents = ARRAY_LENGTH(events); |
| 1019 |
|
| 1020 |
t = &tests[i]; |
| 1021 |
success = parse_evcode_property(t->prop, events, &nevents); |
| 1022 |
litest_assert(success == t->success); |
| 1023 |
if (!success) |
| 1024 |
continue; |
| 1025 |
|
| 1026 |
litest_assert_int_eq(nevents, t->nevents); |
| 1027 |
for (size_t j = 0; j < nevents; j++) { |
| 1028 |
unsigned int type = events[j].type; |
| 1029 |
unsigned int code = events[j].code; |
| 1030 |
int value = events[j].value; |
| 1031 |
litest_assert_int_eq(t->events[j].type, type); |
| 1032 |
litest_assert_int_eq(t->events[j].code, code); |
| 1033 |
litest_assert_int_eq(t->events[j].value, value); |
| 1034 |
} |
| 1035 |
} |
| 1036 |
} |
| 1037 |
END_TEST |
| 1038 |
|
| 1039 |
START_TEST(input_prop_parser) |
| 1040 |
{ |
| 1041 |
/* clang-format off */ |
| 1042 |
struct parser_test_val { |
| 1043 |
const char *prop; |
| 1044 |
bool success; |
| 1045 |
size_t nvals; |
| 1046 |
struct input_prop values[20]; |
| 1047 |
} tests[] = { |
| 1048 |
{ "+INPUT_PROP_BUTTONPAD", true, 1, {{ INPUT_PROP_BUTTONPAD, true }}}, |
| 1049 |
{ "+INPUT_PROP_BUTTONPAD;-INPUT_PROP_POINTER", true, 2, |
| 1050 |
{ { INPUT_PROP_BUTTONPAD, true }, |
| 1051 |
{ INPUT_PROP_POINTER, false }}}, |
| 1052 |
{ "+INPUT_PROP_BUTTONPAD;-0x00;+0x03", true, 3, |
| 1053 |
{ { INPUT_PROP_BUTTONPAD, true }, |
| 1054 |
{ INPUT_PROP_POINTER, false }, |
| 1055 |
{ INPUT_PROP_SEMI_MT, true }}}, |
| 1056 |
{ .prop = "", .success = false }, |
| 1057 |
{ .prop = "0xff", .success = false }, |
| 1058 |
{ .prop = "INPUT_PROP", .success = false }, |
| 1059 |
{ .prop = "INPUT_PROP_FOO", .success = false }, |
| 1060 |
{ .prop = "INPUT_PROP_FOO;INPUT_PROP_FOO", .success = false }, |
| 1061 |
{ .prop = "INPUT_PROP_POINTER;INPUT_PROP_FOO", .success = false }, |
| 1062 |
{ .prop = "none", .success = false }, |
| 1063 |
{ .prop = NULL }, |
| 1064 |
}; |
| 1065 |
/* clang-format on */ |
| 1066 |
struct parser_test_val *t; |
| 1067 |
|
| 1068 |
for (int i = 0; tests[i].prop; i++) { |
| 1069 |
bool success; |
| 1070 |
struct input_prop props[32]; |
| 1071 |
size_t nprops = ARRAY_LENGTH(props); |
| 1072 |
|
| 1073 |
t = &tests[i]; |
| 1074 |
success = parse_input_prop_property(t->prop, props, &nprops); |
| 1075 |
litest_assert(success == t->success); |
| 1076 |
if (!success) |
| 1077 |
continue; |
| 1078 |
|
| 1079 |
litest_assert_int_eq(nprops, t->nvals); |
| 1080 |
for (size_t j = 0; j < t->nvals; j++) { |
| 1081 |
litest_assert_int_eq(t->values[j].prop, props[j].prop); |
| 1082 |
litest_assert_int_eq(t->values[j].enabled, props[j].enabled); |
| 1083 |
} |
| 1084 |
} |
| 1085 |
} |
| 1086 |
END_TEST |
| 1087 |
|
| 1088 |
START_TEST(evdev_abs_parser) |
| 1089 |
{ |
| 1090 |
/* clang-format off */ |
| 1091 |
struct test { |
| 1092 |
uint32_t which; |
| 1093 |
const char *prop; |
| 1094 |
int min, max, res, fuzz, flat; |
| 1095 |
|
| 1096 |
} tests[] = { |
| 1097 |
{ .which = (ABS_MASK_MIN|ABS_MASK_MAX), |
| 1098 |
.prop = "1:2", |
| 1099 |
.min = 1, .max = 2 }, |
| 1100 |
{ .which = (ABS_MASK_MIN|ABS_MASK_MAX), |
| 1101 |
.prop = "1:2:", |
| 1102 |
.min = 1, .max = 2 }, |
| 1103 |
{ .which = (ABS_MASK_MIN|ABS_MASK_MAX|ABS_MASK_RES), |
| 1104 |
.prop = "10:20:30", |
| 1105 |
.min = 10, .max = 20, .res = 30 }, |
| 1106 |
{ .which = (ABS_MASK_RES), |
| 1107 |
.prop = "::100", |
| 1108 |
.res = 100 }, |
| 1109 |
{ .which = (ABS_MASK_MIN), |
| 1110 |
.prop = "10:", |
| 1111 |
.min = 10 }, |
| 1112 |
{ .which = (ABS_MASK_MAX|ABS_MASK_RES), |
| 1113 |
.prop = ":10:1001", |
| 1114 |
.max = 10, .res = 1001 }, |
| 1115 |
{ .which = (ABS_MASK_MIN|ABS_MASK_MAX|ABS_MASK_RES|ABS_MASK_FUZZ), |
| 1116 |
.prop = "1:2:3:4", |
| 1117 |
.min = 1, .max = 2, .res = 3, .fuzz = 4}, |
| 1118 |
{ .which = (ABS_MASK_MIN|ABS_MASK_MAX|ABS_MASK_RES|ABS_MASK_FUZZ|ABS_MASK_FLAT), |
| 1119 |
.prop = "1:2:3:4:5", |
| 1120 |
.min = 1, .max = 2, .res = 3, .fuzz = 4, .flat = 5}, |
| 1121 |
{ .which = (ABS_MASK_MIN|ABS_MASK_RES|ABS_MASK_FUZZ|ABS_MASK_FLAT), |
| 1122 |
.prop = "1::3:4:50", |
| 1123 |
.min = 1, .res = 3, .fuzz = 4, .flat = 50}, |
| 1124 |
{ .which = ABS_MASK_FUZZ|ABS_MASK_FLAT, |
| 1125 |
.prop = ":::5:60", |
| 1126 |
.fuzz = 5, .flat = 60}, |
| 1127 |
{ .which = ABS_MASK_FUZZ, |
| 1128 |
.prop = ":::5:", |
| 1129 |
.fuzz = 5 }, |
| 1130 |
{ .which = ABS_MASK_RES, .prop = "::12::", |
| 1131 |
.res = 12 }, |
| 1132 |
/* Malformed property but parsing this one makes us more |
| 1133 |
* future proof */ |
| 1134 |
{ .which = (ABS_MASK_RES|ABS_MASK_FUZZ|ABS_MASK_FLAT), |
| 1135 |
.prop = "::12:1:2:3:4:5:6", |
| 1136 |
.res = 12, .fuzz = 1, .flat = 2 }, |
| 1137 |
{ .which = 0, .prop = ":::::" }, |
| 1138 |
{ .which = 0, .prop = ":" }, |
| 1139 |
{ .which = 0, .prop = "" }, |
| 1140 |
{ .which = 0, .prop = ":asb::::" }, |
| 1141 |
{ .which = 0, .prop = "foo" }, |
| 1142 |
}; |
| 1143 |
/* clang-format on */ |
| 1144 |
|
| 1145 |
ARRAY_FOR_EACH(tests, t) { |
| 1146 |
struct input_absinfo abs; |
| 1147 |
uint32_t mask; |
| 1148 |
|
| 1149 |
mask = parse_evdev_abs_prop(t->prop, &abs); |
| 1150 |
litest_assert_int_eq(mask, t->which); |
| 1151 |
|
| 1152 |
if (t->which & ABS_MASK_MIN) |
| 1153 |
litest_assert_int_eq(abs.minimum, t->min); |
| 1154 |
if (t->which & ABS_MASK_MAX) |
| 1155 |
litest_assert_int_eq(abs.maximum, t->max); |
| 1156 |
if (t->which & ABS_MASK_RES) |
| 1157 |
litest_assert_int_eq(abs.resolution, t->res); |
| 1158 |
if (t->which & ABS_MASK_FUZZ) |
| 1159 |
litest_assert_int_eq(abs.fuzz, t->fuzz); |
| 1160 |
if (t->which & ABS_MASK_FLAT) |
| 1161 |
litest_assert_int_eq(abs.flat, t->flat); |
| 1162 |
} |
| 1163 |
} |
| 1164 |
END_TEST |
| 1165 |
|
| 1166 |
START_TEST(time_conversion) |
| 1167 |
{ |
| 1168 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_uint64_t(0)), 0U); |
| 1169 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_uint64_t(12345)), 12345U); |
| 1170 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_uint64_t(999999)), 999999U); |
| 1171 |
|
| 1172 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_millis(0)), 0U); |
| 1173 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_millis(1)), 1000U); |
| 1174 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_millis(10)), 10000U); |
| 1175 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_millis(1000)), 1000000U); |
| 1176 |
|
| 1177 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_seconds(0)), 0U); |
| 1178 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_seconds(1)), 1000000U); |
| 1179 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_seconds(10)), 10000000U); |
| 1180 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_seconds(60)), 60000000U); |
| 1181 |
|
| 1182 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_hours(0)), 0ULL); |
| 1183 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_hours(1)), 3600000000ULL); |
| 1184 |
|
| 1185 |
{ |
| 1186 |
struct timeval tv = { .tv_sec = 0, .tv_usec = 0 }; |
| 1187 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_timeval(&tv)), 0U); |
| 1188 |
|
| 1189 |
tv.tv_sec = 1; |
| 1190 |
tv.tv_usec = 0; |
| 1191 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_timeval(&tv)), |
| 1192 |
1000000U); |
| 1193 |
|
| 1194 |
tv.tv_sec = 1; |
| 1195 |
tv.tv_usec = 234567; |
| 1196 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_timeval(&tv)), |
| 1197 |
1234567U); |
| 1198 |
|
| 1199 |
tv.tv_sec = 0; |
| 1200 |
tv.tv_usec = 999999; |
| 1201 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_timeval(&tv)), 999999U); |
| 1202 |
} |
| 1203 |
|
| 1204 |
{ |
| 1205 |
struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 }; |
| 1206 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_timespec(&ts)), 0U); |
| 1207 |
|
| 1208 |
ts.tv_sec = 1; |
| 1209 |
ts.tv_nsec = 0; |
| 1210 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_timespec(&ts)), |
| 1211 |
1000000U); |
| 1212 |
|
| 1213 |
ts.tv_sec = 1; |
| 1214 |
ts.tv_nsec = 234567000; |
| 1215 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_timespec(&ts)), |
| 1216 |
1234567U); |
| 1217 |
|
| 1218 |
ts.tv_sec = 0; |
| 1219 |
ts.tv_nsec = 999999000; |
| 1220 |
litest_assert_int_eq(usec_as_uint64_t(usec_from_timespec(&ts)), |
| 1221 |
999999U); |
| 1222 |
} |
| 1223 |
|
| 1224 |
litest_assert_int_eq(usec_to_millis(usec_from_uint64_t(0)), 0U); |
| 1225 |
litest_assert_int_eq(usec_to_millis(usec_from_uint64_t(1000)), 1U); |
| 1226 |
litest_assert_int_eq(usec_to_millis(usec_from_uint64_t(10000)), 10U); |
| 1227 |
litest_assert_int_eq(usec_to_millis(usec_from_uint64_t(123456)), 123U); |
| 1228 |
litest_assert_int_eq(usec_to_millis(usec_from_uint64_t(999)), 0U); |
| 1229 |
|
| 1230 |
litest_assert_int_eq(usec_to_seconds(usec_from_uint64_t(0)), 0U); |
| 1231 |
litest_assert_int_eq(usec_to_seconds(usec_from_uint64_t(1000000)), 1U); |
| 1232 |
litest_assert_int_eq(usec_to_seconds(usec_from_uint64_t(5000000)), 5U); |
| 1233 |
litest_assert_int_eq(usec_to_seconds(usec_from_uint64_t(60000000)), 60U); |
| 1234 |
litest_assert_int_eq(usec_to_seconds(usec_from_uint64_t(999999)), 0U); |
| 1235 |
|
| 1236 |
litest_assert_int_eq(usec_to_minutes(usec_from_uint64_t(0)), 0U); |
| 1237 |
litest_assert_int_eq(usec_to_minutes(usec_from_uint64_t(60000000)), 1U); |
| 1238 |
litest_assert_int_eq(usec_to_minutes(usec_from_uint64_t(120000000)), 2U); |
| 1239 |
litest_assert_int_eq(usec_to_minutes(usec_from_uint64_t(3600000000)), 60U); |
| 1240 |
litest_assert_int_eq(usec_to_minutes(usec_from_uint64_t(59999999)), 0U); |
| 1241 |
|
| 1242 |
litest_assert_int_eq(usec_to_hours(usec_from_uint64_t(0)), 0U); |
| 1243 |
litest_assert_int_eq(usec_to_hours(usec_from_uint64_t(3600000000)), 1U); |
| 1244 |
litest_assert_int_eq(usec_to_hours(usec_from_uint64_t(7200000000)), 2U); |
| 1245 |
litest_assert_int_eq(usec_to_hours(usec_from_uint64_t(86400000000)), 24U); |
| 1246 |
litest_assert_int_eq(usec_to_hours(usec_from_uint64_t(3599999999)), 0U); |
| 1247 |
|
| 1248 |
{ |
| 1249 |
struct timeval tv; |
| 1250 |
|
| 1251 |
tv = usec_to_timeval(usec_from_uint64_t(0)); |
| 1252 |
litest_assert_int_eq(tv.tv_sec, 0); |
| 1253 |
litest_assert_int_eq(tv.tv_usec, 0); |
| 1254 |
|
| 1255 |
tv = usec_to_timeval(usec_from_uint64_t(1000000)); |
| 1256 |
litest_assert_int_eq(tv.tv_sec, 1); |
| 1257 |
litest_assert_int_eq(tv.tv_usec, 0); |
| 1258 |
|
| 1259 |
tv = usec_to_timeval(usec_from_uint64_t(1234567)); |
| 1260 |
litest_assert_int_eq(tv.tv_sec, 1); |
| 1261 |
litest_assert_int_eq(tv.tv_usec, 234567); |
| 1262 |
|
| 1263 |
tv = usec_to_timeval(usec_from_uint64_t(999999)); |
| 1264 |
litest_assert_int_eq(tv.tv_sec, 0); |
| 1265 |
litest_assert_int_eq(tv.tv_usec, 999999); |
| 1266 |
} |
| 1267 |
|
| 1268 |
{ |
| 1269 |
struct timespec ts; |
| 1270 |
|
| 1271 |
ts = usec_to_timespec(usec_from_uint64_t(0)); |
| 1272 |
litest_assert_int_eq(ts.tv_sec, 0); |
| 1273 |
litest_assert_int_eq(ts.tv_nsec, 0); |
| 1274 |
|
| 1275 |
ts = usec_to_timespec(usec_from_uint64_t(1000000)); |
| 1276 |
litest_assert_int_eq(ts.tv_sec, 1); |
| 1277 |
litest_assert_int_eq(ts.tv_nsec, 0); |
| 1278 |
|
| 1279 |
ts = usec_to_timespec(usec_from_uint64_t(1234567)); |
| 1280 |
litest_assert_int_eq(ts.tv_sec, 1); |
| 1281 |
litest_assert_int_eq(ts.tv_nsec, 234567000); |
| 1282 |
|
| 1283 |
ts = usec_to_timespec(usec_from_uint64_t(999999)); |
| 1284 |
litest_assert_int_eq(ts.tv_sec, 0); |
| 1285 |
litest_assert_int_eq(ts.tv_nsec, 999999000); |
| 1286 |
} |
| 1287 |
} |
| 1288 |
END_TEST |
| 1289 |
|
| 1290 |
START_TEST(human_time) |
| 1291 |
{ |
| 1292 |
/* clang-format off */ |
| 1293 |
struct ht_tests { |
| 1294 |
usec_t interval; |
| 1295 |
unsigned int value; |
| 1296 |
const char *unit; |
| 1297 |
} tests[] = { |
| 1298 |
{ usec_from_uint64_t(0), 0, "us" }, |
| 1299 |
{ usec_from_uint64_t(123), 123, "us" }, |
| 1300 |
{ usec_from_millis(5), 5, "ms" }, |
| 1301 |
{ usec_from_millis(100), 100, "ms" }, |
| 1302 |
{ usec_from_seconds(5), 5, "s" }, |
| 1303 |
{ usec_from_seconds(100), 100, "s" }, |
| 1304 |
{ usec_from_seconds(120), 2, "min" }, |
| 1305 |
{ usec_mul(usec_from_seconds(60), 5), 5, "min" }, |
| 1306 |
{ usec_mul(usec_from_seconds(60), 120), 2, "h" }, |
| 1307 |
{ usec_mul(usec_from_seconds(60), 5 * 60), 5, "h" }, |
| 1308 |
{ usec_mul(usec_from_seconds(60), 48 * 60), 2, "d" }, |
| 1309 |
{ usec_mul(usec_from_seconds(60), 1000 * 24 * 60), 1000, "d" }, |
| 1310 |
{ usec_from_uint64_t(0), 0, NULL }, |
| 1311 |
}; |
| 1312 |
/* clang-format on */ |
| 1313 |
for (int i = 0; tests[i].unit != NULL; i++) { |
| 1314 |
struct human_time ht; |
| 1315 |
|
| 1316 |
ht = to_human_time(tests[i].interval); |
| 1317 |
litest_assert_int_eq(ht.value, tests[i].value); |
| 1318 |
litest_assert_str_eq(ht.unit, tests[i].unit); |
| 1319 |
} |
| 1320 |
} |
| 1321 |
END_TEST |
| 1322 |
|
| 1323 |
struct atoi_test { |
| 1324 |
char *str; |
| 1325 |
bool success; |
| 1326 |
int val; |
| 1327 |
}; |
| 1328 |
|
| 1329 |
START_TEST(safe_atoi_test) |
| 1330 |
{ |
| 1331 |
/* clang-format off */ |
| 1332 |
struct atoi_test tests[] = { |
| 1333 |
{ "10", true, 10 }, |
| 1334 |
{ "20", true, 20 }, |
| 1335 |
{ "-1", true, -1 }, |
| 1336 |
{ "2147483647", true, 2147483647 }, |
| 1337 |
{ "-2147483648", true, -2147483648 }, |
| 1338 |
{ "4294967295", false, 0 }, |
| 1339 |
{ "0x0", false, 0 }, |
| 1340 |
{ "-10x10", false, 0 }, |
| 1341 |
{ "1x-99", false, 0 }, |
| 1342 |
{ "", false, 0 }, |
| 1343 |
{ "abd", false, 0 }, |
| 1344 |
{ "xabd", false, 0 }, |
| 1345 |
{ "0xaf", false, 0 }, |
| 1346 |
{ "0x0x", false, 0 }, |
| 1347 |
{ "x10", false, 0 }, |
| 1348 |
{ NULL, false, 0 }, |
| 1349 |
}; |
| 1350 |
/* clang-format on */ |
| 1351 |
int v; |
| 1352 |
bool success; |
| 1353 |
|
| 1354 |
for (int i = 0; tests[i].str != NULL; i++) { |
| 1355 |
v = 0xad; |
| 1356 |
success = safe_atoi(tests[i].str, &v); |
| 1357 |
litest_assert(success == tests[i].success); |
| 1358 |
if (success) |
| 1359 |
litest_assert_int_eq(v, tests[i].val); |
| 1360 |
else |
| 1361 |
litest_assert_int_eq(v, 0xad); |
| 1362 |
} |
| 1363 |
} |
| 1364 |
END_TEST |
| 1365 |
|
| 1366 |
START_TEST(safe_atoi_base_16_test) |
| 1367 |
{ |
| 1368 |
/* clang-format off */ |
| 1369 |
struct atoi_test tests[] = { |
| 1370 |
{ "10", true, 0x10 }, |
| 1371 |
{ "20", true, 0x20 }, |
| 1372 |
{ "-1", true, -1 }, |
| 1373 |
{ "0x10", true, 0x10 }, |
| 1374 |
{ "0xff", true, 0xff }, |
| 1375 |
{ "abc", true, 0xabc }, |
| 1376 |
{ "-10", true, -0x10 }, |
| 1377 |
{ "0x0", true, 0 }, |
| 1378 |
{ "0", true, 0 }, |
| 1379 |
{ "0x-99", false, 0 }, |
| 1380 |
{ "0xak", false, 0 }, |
| 1381 |
{ "0x", false, 0 }, |
| 1382 |
{ "x10", false, 0 }, |
| 1383 |
{ NULL, false, 0 }, |
| 1384 |
}; |
| 1385 |
/* clang-format on */ |
| 1386 |
|
| 1387 |
int v; |
| 1388 |
bool success; |
| 1389 |
|
| 1390 |
for (int i = 0; tests[i].str != NULL; i++) { |
| 1391 |
v = 0xad; |
| 1392 |
success = safe_atoi_base(tests[i].str, &v, 16); |
| 1393 |
litest_assert(success == tests[i].success); |
| 1394 |
if (success) |
| 1395 |
litest_assert_int_eq(v, tests[i].val); |
| 1396 |
else |
| 1397 |
litest_assert_int_eq(v, 0xad); |
| 1398 |
} |
| 1399 |
} |
| 1400 |
END_TEST |
| 1401 |
|
| 1402 |
START_TEST(safe_atoi_base_8_test) |
| 1403 |
{ |
| 1404 |
/* clang-format off */ |
| 1405 |
struct atoi_test tests[] = { |
| 1406 |
{ "7", true, 07 }, |
| 1407 |
{ "10", true, 010 }, |
| 1408 |
{ "20", true, 020 }, |
| 1409 |
{ "-1", true, -1 }, |
| 1410 |
{ "010", true, 010 }, |
| 1411 |
{ "0ff", false, 0 }, |
| 1412 |
{ "abc", false, 0}, |
| 1413 |
{ "0xabc", false, 0}, |
| 1414 |
{ "-10", true, -010 }, |
| 1415 |
{ "0", true, 0 }, |
| 1416 |
{ "00", true, 0 }, |
| 1417 |
{ "0x0", false, 0 }, |
| 1418 |
{ "0x-99", false, 0 }, |
| 1419 |
{ "0xak", false, 0 }, |
| 1420 |
{ "0x", false, 0 }, |
| 1421 |
{ "x10", false, 0 }, |
| 1422 |
{ NULL, false, 0 }, |
| 1423 |
}; |
| 1424 |
/* clang-format on */ |
| 1425 |
|
| 1426 |
int v; |
| 1427 |
bool success; |
| 1428 |
|
| 1429 |
for (int i = 0; tests[i].str != NULL; i++) { |
| 1430 |
v = 0xad; |
| 1431 |
success = safe_atoi_base(tests[i].str, &v, 8); |
| 1432 |
litest_assert(success == tests[i].success); |
| 1433 |
if (success) |
| 1434 |
litest_assert_int_eq(v, tests[i].val); |
| 1435 |
else |
| 1436 |
litest_assert_int_eq(v, 0xad); |
| 1437 |
} |
| 1438 |
} |
| 1439 |
END_TEST |
| 1440 |
|
| 1441 |
struct atou_test { |
| 1442 |
char *str; |
| 1443 |
bool success; |
| 1444 |
unsigned int val; |
| 1445 |
}; |
| 1446 |
|
| 1447 |
START_TEST(safe_atou_test) |
| 1448 |
{ |
| 1449 |
/* clang-format off */ |
| 1450 |
struct atou_test tests[] = { |
| 1451 |
{ "10", true, 10 }, |
| 1452 |
{ "20", true, 20 }, |
| 1453 |
{ "-1", false, 0 }, |
| 1454 |
{ "2147483647", true, 2147483647 }, |
| 1455 |
{ "-2147483648", false, 0}, |
| 1456 |
{ "0x0", false, 0 }, |
| 1457 |
{ "-10x10", false, 0 }, |
| 1458 |
{ "1x-99", false, 0 }, |
| 1459 |
{ "", false, 0 }, |
| 1460 |
{ "abd", false, 0 }, |
| 1461 |
{ "xabd", false, 0 }, |
| 1462 |
{ "0xaf", false, 0 }, |
| 1463 |
{ "0x0x", false, 0 }, |
| 1464 |
{ "x10", false, 0 }, |
| 1465 |
{ NULL, false, 0 }, |
| 1466 |
}; |
| 1467 |
/* clang-format on */ |
| 1468 |
unsigned int v; |
| 1469 |
bool success; |
| 1470 |
|
| 1471 |
for (int i = 0; tests[i].str != NULL; i++) { |
| 1472 |
v = 0xad; |
| 1473 |
success = safe_atou(tests[i].str, &v); |
| 1474 |
litest_assert(success == tests[i].success); |
| 1475 |
if (success) |
| 1476 |
litest_assert_int_eq(v, tests[i].val); |
| 1477 |
else |
| 1478 |
litest_assert_int_eq(v, 0xadU); |
| 1479 |
} |
| 1480 |
} |
| 1481 |
END_TEST |
| 1482 |
|
| 1483 |
START_TEST(safe_atou_base_16_test) |
| 1484 |
{ |
| 1485 |
/* clang-format off */ |
| 1486 |
struct atou_test tests[] = { |
| 1487 |
{ "10", true, 0x10 }, |
| 1488 |
{ "20", true, 0x20 }, |
| 1489 |
{ "-1", false, 0 }, |
| 1490 |
{ "0x10", true, 0x10 }, |
| 1491 |
{ "0xff", true, 0xff }, |
| 1492 |
{ "abc", true, 0xabc }, |
| 1493 |
{ "-10", false, 0 }, |
| 1494 |
{ "0x0", true, 0 }, |
| 1495 |
{ "0", true, 0 }, |
| 1496 |
{ "0x-99", false, 0 }, |
| 1497 |
{ "0xak", false, 0 }, |
| 1498 |
{ "0x", false, 0 }, |
| 1499 |
{ "x10", false, 0 }, |
| 1500 |
{ NULL, false, 0 }, |
| 1501 |
}; |
| 1502 |
/* clang-format on */ |
| 1503 |
|
| 1504 |
unsigned int v; |
| 1505 |
bool success; |
| 1506 |
|
| 1507 |
for (int i = 0; tests[i].str != NULL; i++) { |
| 1508 |
v = 0xad; |
| 1509 |
success = safe_atou_base(tests[i].str, &v, 16); |
| 1510 |
litest_assert(success == tests[i].success); |
| 1511 |
if (success) |
| 1512 |
litest_assert_int_eq(v, tests[i].val); |
| 1513 |
else |
| 1514 |
litest_assert_int_eq(v, 0xadU); |
| 1515 |
} |
| 1516 |
} |
| 1517 |
END_TEST |
| 1518 |
|
| 1519 |
START_TEST(safe_atou_base_8_test) |
| 1520 |
{ |
| 1521 |
/* clang-format off */ |
| 1522 |
struct atou_test tests[] = { |
| 1523 |
{ "7", true, 07 }, |
| 1524 |
{ "10", true, 010 }, |
| 1525 |
{ "20", true, 020 }, |
| 1526 |
{ "-1", false, 0 }, |
| 1527 |
{ "010", true, 010 }, |
| 1528 |
{ "0ff", false, 0 }, |
| 1529 |
{ "abc", false, 0}, |
| 1530 |
{ "0xabc", false, 0}, |
| 1531 |
{ "-10", false, 0 }, |
| 1532 |
{ "0", true, 0 }, |
| 1533 |
{ "00", true, 0 }, |
| 1534 |
{ "0x0", false, 0 }, |
| 1535 |
{ "0x-99", false, 0 }, |
| 1536 |
{ "0xak", false, 0 }, |
| 1537 |
{ "0x", false, 0 }, |
| 1538 |
{ "x10", false, 0 }, |
| 1539 |
{ NULL, false, 0 }, |
| 1540 |
}; |
| 1541 |
/* clang-format on */ |
| 1542 |
|
| 1543 |
unsigned int v; |
| 1544 |
bool success; |
| 1545 |
|
| 1546 |
for (int i = 0; tests[i].str != NULL; i++) { |
| 1547 |
v = 0xad; |
| 1548 |
success = safe_atou_base(tests[i].str, &v, 8); |
| 1549 |
litest_assert(success == tests[i].success); |
| 1550 |
if (success) |
| 1551 |
litest_assert_int_eq(v, tests[i].val); |
| 1552 |
else |
| 1553 |
litest_assert_int_eq(v, 0xadU); |
| 1554 |
} |
| 1555 |
} |
| 1556 |
END_TEST |
| 1557 |
|
| 1558 |
struct atou64_test { |
| 1559 |
char *str; |
| 1560 |
bool success; |
| 1561 |
uint64_t val; |
| 1562 |
}; |
| 1563 |
|
| 1564 |
START_TEST(safe_atou64_test) |
| 1565 |
{ |
| 1566 |
/* clang-format off */ |
| 1567 |
struct atou64_test tests[] = { |
| 1568 |
{ "10", true, 10 }, |
| 1569 |
{ "20", true, 20 }, |
| 1570 |
{ "-1", false, 0 }, |
| 1571 |
{ "9999999999", true, 9999999999 }, |
| 1572 |
{ "2147483647", true, 2147483647 }, |
| 1573 |
{ "-2147483648", false, 0}, |
| 1574 |
{ "0x0", false, 0 }, |
| 1575 |
{ "-10x10", false, 0 }, |
| 1576 |
{ "1x-99", false, 0 }, |
| 1577 |
{ "", false, 0 }, |
| 1578 |
{ "abd", false, 0 }, |
| 1579 |
{ "xabd", false, 0 }, |
| 1580 |
{ "0xaf", false, 0 }, |
| 1581 |
{ "0x0x", false, 0 }, |
| 1582 |
{ "x10", false, 0 }, |
| 1583 |
{ NULL, false, 0 }, |
| 1584 |
}; |
| 1585 |
/* clang-format on */ |
| 1586 |
uint64_t v; |
| 1587 |
bool success; |
| 1588 |
|
| 1589 |
for (int i = 0; tests[i].str != NULL; i++) { |
| 1590 |
v = 0xad; |
| 1591 |
success = safe_atou64(tests[i].str, &v); |
| 1592 |
litest_assert(success == tests[i].success); |
| 1593 |
if (success) |
| 1594 |
litest_assert_int_eq(v, tests[i].val); |
| 1595 |
else |
| 1596 |
litest_assert_int_eq(v, 0xadU); |
| 1597 |
} |
| 1598 |
} |
| 1599 |
END_TEST |
| 1600 |
|
| 1601 |
START_TEST(safe_atod_test) |
| 1602 |
{ |
| 1603 |
/* clang-format off */ |
| 1604 |
struct atod_test { |
| 1605 |
char *str; |
| 1606 |
bool success; |
| 1607 |
double val; |
| 1608 |
} tests[] = { |
| 1609 |
{ "10", true, 10 }, |
| 1610 |
{ "20", true, 20 }, |
| 1611 |
{ "-1", true, -1 }, |
| 1612 |
{ "2147483647", true, 2147483647 }, |
| 1613 |
{ "-2147483648", true, -2147483648 }, |
| 1614 |
{ "4294967295", true, 4294967295 }, |
| 1615 |
{ "0x0", false, 0 }, |
| 1616 |
{ "0x10", false, 0 }, |
| 1617 |
{ "0xaf", false, 0 }, |
| 1618 |
{ "x80", false, 0 }, |
| 1619 |
{ "0.0", true, 0.0 }, |
| 1620 |
{ "0.1", true, 0.1 }, |
| 1621 |
{ "1.2", true, 1.2 }, |
| 1622 |
{ "-324.9", true, -324.9 }, |
| 1623 |
{ "9324.9", true, 9324.9 }, |
| 1624 |
{ "NAN", false, 0 }, |
| 1625 |
{ "INFINITY", false, 0 }, |
| 1626 |
{ "-10x10", false, 0 }, |
| 1627 |
{ "1x-99", false, 0 }, |
| 1628 |
{ "", false, 0 }, |
| 1629 |
{ "abd", false, 0 }, |
| 1630 |
{ "xabd", false, 0 }, |
| 1631 |
{ "0x0x", false, 0 }, |
| 1632 |
{ NULL, false, 0 }, |
| 1633 |
}; |
| 1634 |
/* clang-format on */ |
| 1635 |
double v; |
| 1636 |
bool success; |
| 1637 |
|
| 1638 |
for (int i = 0; tests[i].str != NULL; i++) { |
| 1639 |
v = 0xad; |
| 1640 |
success = safe_atod(tests[i].str, &v); |
| 1641 |
litest_assert(success == tests[i].success); |
| 1642 |
if (success) |
| 1643 |
litest_assert_double_eq(v, tests[i].val); |
| 1644 |
else |
| 1645 |
litest_assert_int_eq(v, 0xad); |
| 1646 |
} |
| 1647 |
} |
| 1648 |
END_TEST |
| 1649 |
|
| 1650 |
START_TEST(strsplit_test) |
| 1651 |
{ |
| 1652 |
/* clang-format off */ |
| 1653 |
struct strsplit_test { |
| 1654 |
const char *string; |
| 1655 |
const char *delim; |
| 1656 |
const char *results[10]; |
| 1657 |
const size_t nresults; |
| 1658 |
} tests[] = { |
| 1659 |
{ "one two three", " ", { "one", "two", "three", NULL }, 3 }, |
| 1660 |
{ "one two\tthree", " \t", { "one", "two", "three", NULL }, 3 }, |
| 1661 |
{ "one", " ", { "one", NULL }, 1 }, |
| 1662 |
{ "one two ", " ", { "one", "two", NULL }, 2 }, |
| 1663 |
{ "one two", " ", { "one", "two", NULL }, 2 }, |
| 1664 |
{ " one two", " ", { "one", "two", NULL }, 2 }, |
| 1665 |
{ "one", "\t \r", { "one", NULL }, 1 }, |
| 1666 |
{ "one two three", " t", { "one", "wo", "hree", NULL }, 3 }, |
| 1667 |
{ " one two three", "te", { " on", " ", "wo ", "hr", NULL }, 4 }, |
| 1668 |
{ "one", "ne", { "o", NULL }, 1 }, |
| 1669 |
{ "onene", "ne", { "o", NULL }, 1 }, |
| 1670 |
{ "+1-2++3--4++-+5-+-", "+-", { "1", "2", "3", "4", "5", NULL }, 5 }, |
| 1671 |
/* special cases */ |
| 1672 |
{ "", " ", { NULL }, 0 }, |
| 1673 |
{ " ", " ", { NULL }, 0 }, |
| 1674 |
{ " ", " ", { NULL }, 0 }, |
| 1675 |
{ "oneoneone", "one", { NULL} , 0 }, |
| 1676 |
{ NULL, NULL, { NULL }, 0}, |
| 1677 |
}; |
| 1678 |
/* clang-format on */ |
| 1679 |
struct strsplit_test *t = tests; |
| 1680 |
|
| 1681 |
while (t->string) { |
| 1682 |
size_t nelem; |
| 1683 |
char **strv = strv_from_string(t->string, t->delim, &nelem); |
| 1684 |
|
| 1685 |
for (size_t idx = 0; idx < t->nresults; idx++) |
| 1686 |
litest_assert_str_eq(t->results[idx], strv[idx]); |
| 1687 |
|
| 1688 |
litest_assert_int_eq(nelem, t->nresults); |
| 1689 |
|
| 1690 |
/* When there are no elements validate return value is Null, |
| 1691 |
otherwise validate result array is Null terminated. */ |
| 1692 |
if (t->nresults == 0) |
| 1693 |
litest_assert_ptr_eq(strv, NULL); |
| 1694 |
else |
| 1695 |
litest_assert_ptr_eq(strv[t->nresults], NULL); |
| 1696 |
|
| 1697 |
strv_free(strv); |
| 1698 |
t++; |
| 1699 |
} |
| 1700 |
} |
| 1701 |
END_TEST |
| 1702 |
|
| 1703 |
struct strv_test_data { |
| 1704 |
const char *terminate_at; |
| 1705 |
unsigned char bitmask[1]; |
| 1706 |
}; |
| 1707 |
|
| 1708 |
static int |
| 1709 |
strv_test_set_bitmask(const char *str, size_t index, void *data) |
| 1710 |
{ |
| 1711 |
struct strv_test_data *td = data; |
| 1712 |
|
| 1713 |
if (streq(str, td->terminate_at)) |
| 1714 |
return index + 1; |
| 1715 |
|
| 1716 |
set_bit(td->bitmask, index); |
| 1717 |
|
| 1718 |
return 0; |
| 1719 |
} |
| 1720 |
|
| 1721 |
START_TEST(strv_for_each_test) |
| 1722 |
{ |
| 1723 |
/* clang-format off */ |
| 1724 |
struct test_data { |
| 1725 |
const char *terminator; |
| 1726 |
int index; |
| 1727 |
unsigned int bitmask; |
| 1728 |
} test_data[] = { |
| 1729 |
{ "one", 1, 0x0 }, |
| 1730 |
{ "two", 2, 0x1 }, |
| 1731 |
{ "three", 3, 0x3 }, |
| 1732 |
{ "four", 4, 0x7 }, |
| 1733 |
{ "five", 5, 0xf }, |
| 1734 |
{ "does-not-exist", 0, 0x1f }, |
| 1735 |
{ NULL, 0, 0x1f }, |
| 1736 |
{ NULL, 0 }, |
| 1737 |
}; |
| 1738 |
/* clang-format on */ |
| 1739 |
const char *array[] = { "one", "two", "three", "four", "five", NULL }; |
| 1740 |
struct test_data *t = test_data; |
| 1741 |
|
| 1742 |
while (t->terminator || t->bitmask) { |
| 1743 |
const int max = 3; |
| 1744 |
struct strv_test_data td = { |
| 1745 |
.terminate_at = t->terminator, |
| 1746 |
.bitmask = { 0 }, |
| 1747 |
}; |
| 1748 |
|
| 1749 |
int rc = strv_for_each(array, strv_test_set_bitmask, &td); |
| 1750 |
litest_assert_int_eq(rc, t->index); |
| 1751 |
litest_assert_int_eq(td.bitmask[0], t->bitmask); |
| 1752 |
|
| 1753 |
struct strv_test_data tdmax = { |
| 1754 |
.terminate_at = t->terminator, |
| 1755 |
.bitmask = { 0 }, |
| 1756 |
}; |
| 1757 |
|
| 1758 |
rc = strv_for_each_n(array, max, strv_test_set_bitmask, &tdmax); |
| 1759 |
if (max < t->index) |
| 1760 |
litest_assert_int_eq(rc, 0); |
| 1761 |
else |
| 1762 |
litest_assert_int_eq(rc, t->index); |
| 1763 |
litest_assert_int_eq(tdmax.bitmask[0], t->bitmask & ((1 << max) - 1)); |
| 1764 |
|
| 1765 |
t++; |
| 1766 |
} |
| 1767 |
} |
| 1768 |
END_TEST |
| 1769 |
|
| 1770 |
__attribute__((format(printf, 1, 0))) static char ** |
| 1771 |
test_strv_appendv(char *format, ...) |
| 1772 |
{ |
| 1773 |
va_list args; |
| 1774 |
va_start(args, format); |
| 1775 |
char **strv = NULL; |
| 1776 |
strv = strv_append_vprintf(strv, "%s %d", args); |
| 1777 |
va_end(args); |
| 1778 |
return strv; |
| 1779 |
} |
| 1780 |
|
| 1781 |
START_TEST(strv_append_test) |
| 1782 |
{ |
| 1783 |
{ |
| 1784 |
char *test_strv1[] = { "a", "b", "c", NULL }; |
| 1785 |
char **test_strv2 = NULL; |
| 1786 |
|
| 1787 |
litest_assert_int_eq(strv_len(test_strv1), 4U); |
| 1788 |
litest_assert_int_eq(strv_len(test_strv2), 0U); |
| 1789 |
} |
| 1790 |
{ |
| 1791 |
char **strv = NULL; |
| 1792 |
char *dup = safe_strdup("test"); |
| 1793 |
strv = strv_append_take(strv, &dup); |
| 1794 |
litest_assert_ptr_null(dup); |
| 1795 |
litest_assert_ptr_notnull(strv); |
| 1796 |
litest_assert_str_eq(strv[0], "test"); |
| 1797 |
litest_assert_ptr_eq(strv[1], NULL); |
| 1798 |
litest_assert_int_eq(strv_len(strv), 2U); |
| 1799 |
|
| 1800 |
char *dup2 = safe_strdup("test2"); |
| 1801 |
strv = strv_append_take(strv, &dup2); |
| 1802 |
litest_assert_ptr_null(dup2); |
| 1803 |
litest_assert_str_eq(strv[1], "test2"); |
| 1804 |
litest_assert_ptr_eq(strv[2], NULL); |
| 1805 |
litest_assert_int_eq(strv_len(strv), 3U); |
| 1806 |
|
| 1807 |
strv = strv_append_take(strv, NULL); |
| 1808 |
litest_assert_int_eq(strv_len(strv), 3U); |
| 1809 |
strv_free(strv); |
| 1810 |
} |
| 1811 |
{ |
| 1812 |
char **strv = NULL; |
| 1813 |
strv = strv_append_strdup(strv, "banana"); |
| 1814 |
litest_assert(strv != NULL); |
| 1815 |
litest_assert_str_eq(strv[0], "banana"); |
| 1816 |
litest_assert_ptr_null(strv[1]); |
| 1817 |
litest_assert_int_eq(strv_len(strv), 2U); |
| 1818 |
strv_free(strv); |
| 1819 |
} |
| 1820 |
{ |
| 1821 |
char **strv = test_strv_appendv("%s %d", "apple", 2); |
| 1822 |
litest_assert_ptr_notnull(strv); |
| 1823 |
litest_assert_str_eq(strv[0], "apple 2"); |
| 1824 |
litest_assert_ptr_null(strv[1]); |
| 1825 |
litest_assert_int_eq(strv_len(strv), 2U); |
| 1826 |
strv_free(strv); |
| 1827 |
} |
| 1828 |
{ |
| 1829 |
char **strv = NULL; |
| 1830 |
strv = strv_append_printf(strv, "coco%s", "nut"); |
| 1831 |
litest_assert_ptr_notnull(strv); |
| 1832 |
litest_assert_str_eq(strv[0], "coconut"); |
| 1833 |
litest_assert_ptr_null(strv[1]); |
| 1834 |
litest_assert_int_eq(strv_len(strv), 2U); |
| 1835 |
strv_free(strv); |
| 1836 |
} |
| 1837 |
} |
| 1838 |
END_TEST |
| 1839 |
|
| 1840 |
START_TEST(strv_find_test) |
| 1841 |
{ |
| 1842 |
char *strv[] = { "a", "b", "c", NULL }; |
| 1843 |
|
| 1844 |
bool rc; |
| 1845 |
size_t index; |
| 1846 |
|
| 1847 |
rc = strv_find(strv, "a", &index); |
| 1848 |
litest_assert(rc); |
| 1849 |
litest_assert_int_eq(index, 0U); |
| 1850 |
|
| 1851 |
rc = strv_find(strv, "b", &index); |
| 1852 |
litest_assert(rc); |
| 1853 |
litest_assert_int_eq(index, 1U); |
| 1854 |
|
| 1855 |
rc = strv_find(strv, "a", NULL); |
| 1856 |
litest_assert(rc); |
| 1857 |
|
| 1858 |
index = 0xffff; |
| 1859 |
rc = strv_find(strv, "d", &index); |
| 1860 |
litest_assert(!rc); |
| 1861 |
litest_assert_int_eq(index, 0xffffU); |
| 1862 |
|
| 1863 |
rc = strv_find(strv, "d", NULL); |
| 1864 |
litest_assert(!rc); |
| 1865 |
|
| 1866 |
rc = strv_find(NULL, "a", &index); |
| 1867 |
litest_assert(!rc); |
| 1868 |
litest_assert_int_eq(index, 0xffffU); |
| 1869 |
|
| 1870 |
rc = strv_find(NULL, NULL, &index); |
| 1871 |
litest_assert(!rc); |
| 1872 |
litest_assert_int_eq(index, 0xffffU); |
| 1873 |
|
| 1874 |
rc = strv_find(strv, NULL, &index); |
| 1875 |
litest_assert(!rc); |
| 1876 |
litest_assert_int_eq(index, 0xffffU); |
| 1877 |
} |
| 1878 |
END_TEST |
| 1879 |
|
| 1880 |
START_TEST(strv_find_substring_test) |
| 1881 |
{ |
| 1882 |
char *strv[] = { "a", "bc", "cccc", NULL }; |
| 1883 |
|
| 1884 |
bool rc; |
| 1885 |
size_t index; |
| 1886 |
|
| 1887 |
rc = strv_find_substring(strv, "a", &index); |
| 1888 |
litest_assert(rc); |
| 1889 |
litest_assert_int_eq(index, 0U); |
| 1890 |
|
| 1891 |
rc = strv_find_substring(strv, "b", &index); |
| 1892 |
litest_assert(rc); |
| 1893 |
litest_assert_int_eq(index, 1U); |
| 1894 |
|
| 1895 |
rc = strv_find_substring(strv, "c", &index); |
| 1896 |
litest_assert(rc); |
| 1897 |
litest_assert_int_eq(index, 1U); |
| 1898 |
|
| 1899 |
rc = strv_find_substring(strv, "a", NULL); |
| 1900 |
litest_assert(rc); |
| 1901 |
|
| 1902 |
index = 0xffff; |
| 1903 |
rc = strv_find_substring(strv, "d", &index); |
| 1904 |
litest_assert(!rc); |
| 1905 |
litest_assert_int_eq(index, 0xffffU); |
| 1906 |
|
| 1907 |
rc = strv_find_substring(strv, "d", NULL); |
| 1908 |
litest_assert(!rc); |
| 1909 |
|
| 1910 |
rc = strv_find_substring(NULL, "a", &index); |
| 1911 |
litest_assert(!rc); |
| 1912 |
litest_assert_int_eq(index, 0xffffU); |
| 1913 |
|
| 1914 |
rc = strv_find_substring(NULL, NULL, &index); |
| 1915 |
litest_assert(!rc); |
| 1916 |
litest_assert_int_eq(index, 0xffffU); |
| 1917 |
|
| 1918 |
rc = strv_find_substring(strv, NULL, &index); |
| 1919 |
litest_assert(!rc); |
| 1920 |
litest_assert_int_eq(index, 0xffffU); |
| 1921 |
} |
| 1922 |
END_TEST |
| 1923 |
|
| 1924 |
START_TEST(double_array_from_string_test) |
| 1925 |
{ |
| 1926 |
/* clang-format off */ |
| 1927 |
struct double_array_from_string_test { |
| 1928 |
const char *string; |
| 1929 |
const char *delim; |
| 1930 |
const double array[10]; |
| 1931 |
const size_t len; |
| 1932 |
const bool result; |
| 1933 |
} tests[] = { |
| 1934 |
{ "1 2 3", " ", { 1, 2, 3 }, 3 }, |
| 1935 |
{ "1", " ", { 1 }, 1 }, |
| 1936 |
{ "1,2.5,", ",", { 1, 2.5 }, 2 }, |
| 1937 |
{ "1.0 2", " ", { 1, 2.0 }, 2 }, |
| 1938 |
{ " 1 2", " ", { 1, 2 }, 2 }, |
| 1939 |
{ " ; 1;2 3.5 ;;4.1", "; ", { 1, 2, 3.5, 4.1 }, 4 }, |
| 1940 |
/* special cases */ |
| 1941 |
{ "1 two", " ", { 0 }, 0 }, |
| 1942 |
{ "one two", " ", { 0 }, 0 }, |
| 1943 |
{ "one 2", " ", { 0 }, 0 }, |
| 1944 |
{ "", " ", { 0 }, 0 }, |
| 1945 |
{ " ", " ", { 0 }, 0 }, |
| 1946 |
{ " ", " ", { 0 }, 0 }, |
| 1947 |
{ "", " ", { 0 }, 0 }, |
| 1948 |
{ "oneoneone", "one", { 0 }, 0 }, |
| 1949 |
{ NULL, NULL, { 0 }, 0 }, |
| 1950 |
}; |
| 1951 |
/* clang-format on */ |
| 1952 |
struct double_array_from_string_test *t = tests; |
| 1953 |
|
| 1954 |
while (t->string) { |
| 1955 |
size_t len; |
| 1956 |
double *array = double_array_from_string(t->string, t->delim, &len); |
| 1957 |
litest_assert_int_eq(len, t->len); |
| 1958 |
|
| 1959 |
for (size_t idx = 0; idx < len; idx++) { |
| 1960 |
litest_assert_ptr_notnull(array); |
| 1961 |
litest_assert_double_eq(array[idx], t->array[idx]); |
| 1962 |
} |
| 1963 |
|
| 1964 |
free(array); |
| 1965 |
t++; |
| 1966 |
} |
| 1967 |
} |
| 1968 |
END_TEST |
| 1969 |
|
| 1970 |
START_TEST(strargv_test) |
| 1971 |
{ |
| 1972 |
/* clang-format off */ |
| 1973 |
struct argv_test { |
| 1974 |
int argc; |
| 1975 |
char *argv[10]; |
| 1976 |
int expected; |
| 1977 |
} tests[] = { |
| 1978 |
{ 0, {NULL}, 0 }, |
| 1979 |
{ 1, {"hello", "World"}, 1 }, |
| 1980 |
{ 2, {"hello", "World"}, 2 }, |
| 1981 |
{ 2, {"", " "}, 2 }, |
| 1982 |
{ 2, {"", NULL}, 0 }, |
| 1983 |
{ 2, {NULL, NULL}, 0 }, |
| 1984 |
{ 1, {NULL, NULL}, 0 }, |
| 1985 |
{ 3, {"hello", NULL, "World"}, 0 }, |
| 1986 |
}; |
| 1987 |
/* clang-format on */ |
| 1988 |
|
| 1989 |
ARRAY_FOR_EACH(tests, t) { |
| 1990 |
char **strv = strv_from_argv(t->argc, t->argv); |
| 1991 |
|
| 1992 |
if (t->expected == 0) { |
| 1993 |
litest_assert(strv == NULL); |
| 1994 |
} else { |
| 1995 |
int count = 0; |
| 1996 |
char **s = strv; |
| 1997 |
while (*s) { |
| 1998 |
litest_assert_str_eq(*s, t->argv[count]); |
| 1999 |
count++; |
| 2000 |
s++; |
| 2001 |
} |
| 2002 |
litest_assert_int_eq(t->expected, count); |
| 2003 |
strv_free(strv); |
| 2004 |
} |
| 2005 |
} |
| 2006 |
} |
| 2007 |
END_TEST |
| 2008 |
|
| 2009 |
START_TEST(kvsplit_double_test) |
| 2010 |
{ |
| 2011 |
/* clang-format off */ |
| 2012 |
struct kvsplit_dbl_test { |
| 2013 |
const char *string; |
| 2014 |
const char *psep; |
| 2015 |
const char *kvsep; |
| 2016 |
ssize_t nresults; |
| 2017 |
struct { |
| 2018 |
double a; |
| 2019 |
double b; |
| 2020 |
} results[32]; |
| 2021 |
} tests[] = { |
| 2022 |
{ "1:2;3:4;5:6", ";", ":", 3, { {1, 2}, {3, 4}, {5, 6}}}, |
| 2023 |
{ "1.0x2.3 -3.2x4.5 8.090909x-6.00", " ", "x", 3, { {1.0, 2.3}, {-3.2, 4.5}, {8.090909, -6}}}, |
| 2024 |
|
| 2025 |
{ "1:2", "x", ":", 1, {{1, 2}}}, |
| 2026 |
{ "1:2", ":", "x", -1, {}}, |
| 2027 |
{ "1:2", NULL, "x", -1, {}}, |
| 2028 |
{ "1:2", "", "x", -1, {}}, |
| 2029 |
{ "1:2", "x", NULL, -1, {}}, |
| 2030 |
{ "1:2", "x", "", -1, {}}, |
| 2031 |
{ "a:b", "x", ":", -1, {}}, |
| 2032 |
{ "", " ", "x", -1, {}}, |
| 2033 |
{ "1.2.3.4.5", ".", "", -1, {}}, |
| 2034 |
{ NULL }, |
| 2035 |
}; |
| 2036 |
/* clang-format on */ |
| 2037 |
struct kvsplit_dbl_test *t = tests; |
| 2038 |
|
| 2039 |
while (t->string) { |
| 2040 |
struct key_value_double *result = NULL; |
| 2041 |
ssize_t npairs; |
| 2042 |
|
| 2043 |
npairs = kv_double_from_string(t->string, t->psep, t->kvsep, &result); |
| 2044 |
litest_assert_int_eq(npairs, t->nresults); |
| 2045 |
|
| 2046 |
for (ssize_t i = 0; i < npairs; i++) { |
| 2047 |
litest_assert_double_eq(t->results[i].a, result[i].key); |
| 2048 |
litest_assert_double_eq(t->results[i].b, result[i].value); |
| 2049 |
} |
| 2050 |
|
| 2051 |
free(result); |
| 2052 |
t++; |
| 2053 |
} |
| 2054 |
} |
| 2055 |
END_TEST |
| 2056 |
|
| 2057 |
START_TEST(strjoin_test) |
| 2058 |
{ |
| 2059 |
/* clang-format off */ |
| 2060 |
struct strjoin_test { |
| 2061 |
char *strv[10]; |
| 2062 |
const char *joiner; |
| 2063 |
const char *result; |
| 2064 |
} tests[] = { |
| 2065 |
{ { "one", "two", "three", NULL }, " ", "one two three" }, |
| 2066 |
{ { "one", NULL }, "x", "one" }, |
| 2067 |
{ { "one", "two", NULL }, "x", "onextwo" }, |
| 2068 |
{ { "one", "two", NULL }, ",", "one,two" }, |
| 2069 |
{ { "one", "two", NULL }, ", ", "one, two" }, |
| 2070 |
{ { "one", "two", NULL }, "one", "oneonetwo" }, |
| 2071 |
{ { "one", "two", NULL }, NULL, NULL }, |
| 2072 |
{ { "", "", "", NULL }, " ", " " }, |
| 2073 |
{ { "a", "b", "c", NULL }, "", "abc" }, |
| 2074 |
{ { "", "b", "c", NULL }, "x", "xbxc" }, |
| 2075 |
{ { "", "", "", NULL }, "", "" }, |
| 2076 |
{ { NULL }, NULL, NULL }, |
| 2077 |
}; |
| 2078 |
/* clang-format on */ |
| 2079 |
struct strjoin_test *t = tests; |
| 2080 |
struct strjoin_test nulltest = { { NULL }, "x", NULL }; |
| 2081 |
|
| 2082 |
while (t->strv[0]) { |
| 2083 |
char *str; |
| 2084 |
str = strv_join(t->strv, t->joiner); |
| 2085 |
if (t->result == NULL) |
| 2086 |
litest_assert(str == NULL); |
| 2087 |
else |
| 2088 |
litest_assert_str_eq(str, t->result); |
| 2089 |
free(str); |
| 2090 |
t++; |
| 2091 |
} |
| 2092 |
|
| 2093 |
litest_assert(strv_join(nulltest.strv, "x") == NULL); |
| 2094 |
} |
| 2095 |
END_TEST |
| 2096 |
|
| 2097 |
START_TEST(strstrip_test) |
| 2098 |
{ |
| 2099 |
/* clang-format off */ |
| 2100 |
struct strstrip_test { |
| 2101 |
const char *string; |
| 2102 |
const char *expected; |
| 2103 |
const char *what; |
| 2104 |
} tests[] = { |
| 2105 |
{ "foo", "foo", "1234" }, |
| 2106 |
{ "\"bar\"", "bar", "\"" }, |
| 2107 |
{ "'bar'", "bar", "'" }, |
| 2108 |
{ "\"bar\"", "\"bar\"", "'" }, |
| 2109 |
{ "'bar'", "'bar'", "\"" }, |
| 2110 |
{ "\"bar\"", "bar", "\"" }, |
| 2111 |
{ "\"\"", "", "\"" }, |
| 2112 |
{ "\"foo\"bar\"", "foo\"bar", "\"" }, |
| 2113 |
{ "\"'foo\"bar\"", "foo\"bar", "\"'" }, |
| 2114 |
{ "abcfooabcbarbca", "fooabcbar", "abc" }, |
| 2115 |
{ "xxxxfoo", "foo", "x" }, |
| 2116 |
{ "fooyyyy", "foo", "y" }, |
| 2117 |
{ "xxxxfooyyyy", "foo", "xy" }, |
| 2118 |
{ "x xfooy y", " xfooy ", "xy" }, |
| 2119 |
{ " foo\n", "foo", " \n" }, |
| 2120 |
{ "", "", "abc" }, |
| 2121 |
{ "", "", "" }, |
| 2122 |
{ NULL , NULL, NULL }, |
| 2123 |
}; |
| 2124 |
/* clang-format on */ |
| 2125 |
struct strstrip_test *t = tests; |
| 2126 |
|
| 2127 |
while (t->string) { |
| 2128 |
char *str; |
| 2129 |
str = strstrip(t->string, t->what); |
| 2130 |
litest_assert_str_eq(str, t->expected); |
| 2131 |
free(str); |
| 2132 |
t++; |
| 2133 |
} |
| 2134 |
} |
| 2135 |
END_TEST |
| 2136 |
|
| 2137 |
START_TEST(strendswith_test) |
| 2138 |
{ |
| 2139 |
/* clang-format off */ |
| 2140 |
struct strendswith_test { |
| 2141 |
const char *string; |
| 2142 |
const char *suffix; |
| 2143 |
bool expected; |
| 2144 |
} tests[] = { |
| 2145 |
{ "foobar", "bar", true }, |
| 2146 |
{ "foobar", "foo", false }, |
| 2147 |
{ "foobar", "foobar", true }, |
| 2148 |
{ "foo", "foobar", false }, |
| 2149 |
{ "foobar", "", false }, |
| 2150 |
{ "", "", false }, |
| 2151 |
{ "", "foo", false }, |
| 2152 |
{ NULL, NULL, false }, |
| 2153 |
}; |
| 2154 |
/* clang-format on */ |
| 2155 |
|
| 2156 |
for (struct strendswith_test *t = tests; t->string; t++) { |
| 2157 |
litest_assert_int_eq(strendswith(t->string, t->suffix), t->expected); |
| 2158 |
} |
| 2159 |
} |
| 2160 |
END_TEST |
| 2161 |
|
| 2162 |
START_TEST(strstartswith_test) |
| 2163 |
{ |
| 2164 |
/* clang-format off */ |
| 2165 |
struct strstartswith_test { |
| 2166 |
const char *string; |
| 2167 |
const char *suffix; |
| 2168 |
bool expected; |
| 2169 |
} tests[] = { |
| 2170 |
{ "foobar", "foo", true }, |
| 2171 |
{ "foobar", "bar", false }, |
| 2172 |
{ "foobar", "foobar", true }, |
| 2173 |
{ "foo", "foobar", false }, |
| 2174 |
{ "foo", "", false }, |
| 2175 |
{ "", "", false }, |
| 2176 |
{ "foo", "", false }, |
| 2177 |
{ NULL, NULL, false }, |
| 2178 |
}; |
| 2179 |
/* clang-format on */ |
| 2180 |
|
| 2181 |
for (struct strstartswith_test *t = tests; t->string; t++) { |
| 2182 |
litest_assert_int_eq(strstartswith(t->string, t->suffix), t->expected); |
| 2183 |
} |
| 2184 |
} |
| 2185 |
END_TEST |
| 2186 |
|
| 2187 |
START_TEST(strsanitize_test) |
| 2188 |
{ |
| 2189 |
/* clang-format off */ |
| 2190 |
struct strsanitize_test { |
| 2191 |
const char *string; |
| 2192 |
const char *expected; |
| 2193 |
} tests[] = { |
| 2194 |
{ "foobar", "foobar" }, |
| 2195 |
{ "", "" }, |
| 2196 |
{ "%", "%%" }, |
| 2197 |
{ "%%%%", "%%%%%%%%" }, |
| 2198 |
{ "x %s", "x %%s" }, |
| 2199 |
{ "x %", "x %%" }, |
| 2200 |
{ "%sx", "%%sx" }, |
| 2201 |
{ "%s%s", "%%s%%s" }, |
| 2202 |
{ "\t", "?" }, |
| 2203 |
{ "\n", "?" }, |
| 2204 |
{ "\r", "?" }, |
| 2205 |
{ "\x1b[31m", "?[31m" }, |
| 2206 |
{ "foo\tbar", "foo?bar" }, |
| 2207 |
{ "foo\nbar", "foo?bar" }, |
| 2208 |
{ "\x01\x1f\x7f", "???" }, |
| 2209 |
{ "clean", "clean" }, |
| 2210 |
{ "a\x1b[0mb", "a?[0mb" }, |
| 2211 |
{ "%\n", "%%?" }, |
| 2212 |
{ NULL, NULL }, |
| 2213 |
}; |
| 2214 |
/* clang-format on */ |
| 2215 |
|
| 2216 |
for (struct strsanitize_test *t = tests; t->string; t++) { |
| 2217 |
char *sanitized = str_sanitize(t->string); |
| 2218 |
litest_assert_str_eq(sanitized, t->expected); |
| 2219 |
free(sanitized); |
| 2220 |
} |
| 2221 |
} |
| 2222 |
END_TEST |
| 2223 |
|
| 2224 |
START_TEST(list_test_insert) |
| 2225 |
{ |
| 2226 |
struct list_test { |
| 2227 |
int val; |
| 2228 |
struct list node; |
| 2229 |
} tests[] = { |
| 2230 |
{ .val = 1 }, |
| 2231 |
{ .val = 2 }, |
| 2232 |
{ .val = 3 }, |
| 2233 |
{ .val = 4 }, |
| 2234 |
}; |
| 2235 |
struct list_test *t; |
| 2236 |
struct list head; |
| 2237 |
int val; |
| 2238 |
|
| 2239 |
list_init(&head); |
| 2240 |
|
| 2241 |
ARRAY_FOR_EACH(tests, t) { |
| 2242 |
list_insert(&head, &t->node); |
| 2243 |
} |
| 2244 |
|
| 2245 |
val = 4; |
| 2246 |
list_for_each(t, &head, node) { |
| 2247 |
litest_assert_int_eq(t->val, val); |
| 2248 |
val--; |
| 2249 |
} |
| 2250 |
|
| 2251 |
litest_assert_int_eq(val, 0); |
| 2252 |
} |
| 2253 |
END_TEST |
| 2254 |
|
| 2255 |
START_TEST(list_test_append) |
| 2256 |
{ |
| 2257 |
struct list_test { |
| 2258 |
int val; |
| 2259 |
struct list node; |
| 2260 |
} tests[] = { |
| 2261 |
{ .val = 1 }, |
| 2262 |
{ .val = 2 }, |
| 2263 |
{ .val = 3 }, |
| 2264 |
{ .val = 4 }, |
| 2265 |
}; |
| 2266 |
struct list_test *t; |
| 2267 |
struct list head; |
| 2268 |
int val; |
| 2269 |
|
| 2270 |
list_init(&head); |
| 2271 |
|
| 2272 |
ARRAY_FOR_EACH(tests, t) { |
| 2273 |
list_append(&head, &t->node); |
| 2274 |
} |
| 2275 |
|
| 2276 |
val = 1; |
| 2277 |
list_for_each(t, &head, node) { |
| 2278 |
litest_assert_int_eq(t->val, val); |
| 2279 |
val++; |
| 2280 |
} |
| 2281 |
litest_assert_int_eq(val, 5); |
| 2282 |
} |
| 2283 |
END_TEST |
| 2284 |
|
| 2285 |
START_TEST(list_test_chain) |
| 2286 |
{ |
| 2287 |
struct list_test { |
| 2288 |
int val; |
| 2289 |
struct list node; |
| 2290 |
} tests[] = { |
| 2291 |
{ .val = 1 }, |
| 2292 |
{ .val = 2 }, |
| 2293 |
{ .val = 3 }, |
| 2294 |
{ .val = 4 }, |
| 2295 |
}; |
| 2296 |
struct list l1, l2; |
| 2297 |
struct list_test *t; |
| 2298 |
int val; |
| 2299 |
|
| 2300 |
list_init(&l1); |
| 2301 |
list_init(&l2); |
| 2302 |
|
| 2303 |
list_chain(&l1, &l2); |
| 2304 |
litest_assert(list_empty(&l2)); |
| 2305 |
|
| 2306 |
list_append(&l2, &tests[0].node); |
| 2307 |
list_append(&l2, &tests[1].node); |
| 2308 |
list_chain(&l1, &l2); |
| 2309 |
litest_assert(list_empty(&l2)); |
| 2310 |
|
| 2311 |
val = 1; |
| 2312 |
list_for_each_safe(t, &l1, node) { |
| 2313 |
litest_assert_int_eq(t->val, val); |
| 2314 |
val++; |
| 2315 |
list_remove(&t->node); |
| 2316 |
} |
| 2317 |
litest_assert_int_eq(val, 3); |
| 2318 |
|
| 2319 |
list_append(&l1, &tests[0].node); |
| 2320 |
list_append(&l1, &tests[1].node); |
| 2321 |
list_append(&l2, &tests[2].node); |
| 2322 |
list_append(&l2, &tests[3].node); |
| 2323 |
|
| 2324 |
list_chain(&l1, &l2); |
| 2325 |
litest_assert(list_empty(&l2)); |
| 2326 |
|
| 2327 |
val = 1; |
| 2328 |
list_for_each(t, &l1, node) { |
| 2329 |
litest_assert_int_eq(t->val, val); |
| 2330 |
val++; |
| 2331 |
} |
| 2332 |
litest_assert_int_eq(val, 5); |
| 2333 |
} |
| 2334 |
END_TEST |
| 2335 |
|
| 2336 |
START_TEST(list_test_foreach) |
| 2337 |
{ |
| 2338 |
struct list_test { |
| 2339 |
int val; |
| 2340 |
struct list node; |
| 2341 |
} tests[] = { |
| 2342 |
{ .val = 1 }, |
| 2343 |
{ .val = 2 }, |
| 2344 |
{ .val = 3 }, |
| 2345 |
{ .val = 4 }, |
| 2346 |
}; |
| 2347 |
struct list_test *t; |
| 2348 |
struct list head; |
| 2349 |
|
| 2350 |
list_init(&head); |
| 2351 |
|
| 2352 |
ARRAY_FOR_EACH(tests, t) { |
| 2353 |
list_append(&head, &t->node); |
| 2354 |
} |
| 2355 |
|
| 2356 |
/* Make sure both loop macros are a single line statement */ |
| 2357 |
if (false) |
| 2358 |
list_for_each(t, &head, node) { |
| 2359 |
litest_abort_msg("We should not get here"); |
| 2360 |
} |
| 2361 |
|
| 2362 |
if (false) |
| 2363 |
list_for_each_safe(t, &head, node) { |
| 2364 |
litest_abort_msg("We should not get here"); |
| 2365 |
} |
| 2366 |
} |
| 2367 |
END_TEST |
| 2368 |
|
| 2369 |
START_TEST(list_test_first_last) |
| 2370 |
{ |
| 2371 |
struct list_test { |
| 2372 |
int val; |
| 2373 |
struct list node; |
| 2374 |
} tests[] = { |
| 2375 |
{ .val = 1 }, |
| 2376 |
{ .val = 2 }, |
| 2377 |
{ .val = 3 }, |
| 2378 |
{ .val = 4 }, |
| 2379 |
}; |
| 2380 |
struct list head; |
| 2381 |
|
| 2382 |
list_init(&head); |
| 2383 |
|
| 2384 |
ARRAY_FOR_EACH(tests, t) { |
| 2385 |
list_append(&head, &t->node); |
| 2386 |
} |
| 2387 |
|
| 2388 |
struct list_test *first; |
| 2389 |
struct list_test *last; |
| 2390 |
|
| 2391 |
first = list_first_entry(&head, first, node); |
| 2392 |
last = list_last_entry(&head, last, node); |
| 2393 |
litest_assert_ptr_eq(first, &tests[0]); |
| 2394 |
litest_assert_ptr_eq(last, &tests[3]); |
| 2395 |
|
| 2396 |
struct list_test *second; |
| 2397 |
struct list_test *penultimate; |
| 2398 |
|
| 2399 |
second = list_first_entry(&first->node, first, node); |
| 2400 |
penultimate = list_last_entry(&last->node, last, node); |
| 2401 |
litest_assert_ptr_eq(second, &tests[1]); |
| 2402 |
litest_assert_ptr_eq(penultimate, &tests[2]); |
| 2403 |
|
| 2404 |
/* Now remove nodes */ |
| 2405 |
|
| 2406 |
/* No change expected */ |
| 2407 |
list_remove(&tests[2].node); |
| 2408 |
first = list_first_entry(&head, first, node); |
| 2409 |
last = list_last_entry(&head, last, node); |
| 2410 |
litest_assert_ptr_eq(first, &tests[0]); |
| 2411 |
litest_assert_ptr_eq(last, &tests[3]); |
| 2412 |
|
| 2413 |
list_remove(&tests[3].node); |
| 2414 |
first = list_first_entry(&head, first, node); |
| 2415 |
last = list_last_entry(&head, last, node); |
| 2416 |
litest_assert_ptr_eq(first, &tests[0]); |
| 2417 |
litest_assert_ptr_eq(last, &tests[1]); |
| 2418 |
|
| 2419 |
list_remove(&tests[0].node); |
| 2420 |
first = list_first_entry(&head, first, node); |
| 2421 |
last = list_last_entry(&head, last, node); |
| 2422 |
litest_assert_ptr_eq(first, &tests[1]); |
| 2423 |
litest_assert_ptr_eq(last, &tests[1]); |
| 2424 |
} |
| 2425 |
END_TEST |
| 2426 |
|
| 2427 |
START_TEST(strverscmp_test) |
| 2428 |
{ |
| 2429 |
litest_assert_int_eq(libinput_strverscmp("", ""), 0); |
| 2430 |
litest_assert_int_gt(libinput_strverscmp("0.0.1", ""), 0); |
| 2431 |
litest_assert_int_lt(libinput_strverscmp("", "0.0.1"), 0); |
| 2432 |
litest_assert_int_eq(libinput_strverscmp("0.0.1", "0.0.1"), 0); |
| 2433 |
litest_assert_int_eq(libinput_strverscmp("0.0.1", "0.0.2"), -1); |
| 2434 |
litest_assert_int_eq(libinput_strverscmp("0.0.2", "0.0.1"), 1); |
| 2435 |
litest_assert_int_eq(libinput_strverscmp("0.0.1", "0.1.0"), -1); |
| 2436 |
litest_assert_int_eq(libinput_strverscmp("0.1.0", "0.0.1"), 1); |
| 2437 |
} |
| 2438 |
END_TEST |
| 2439 |
|
| 2440 |
START_TEST(streq_test) |
| 2441 |
{ |
| 2442 |
litest_assert(streq("", "") == true); |
| 2443 |
litest_assert(streq(NULL, NULL) == true); |
| 2444 |
litest_assert(streq("0.0.1", "") == false); |
| 2445 |
litest_assert(streq("foo", NULL) == false); |
| 2446 |
litest_assert(streq(NULL, "foo") == false); |
| 2447 |
litest_assert(streq("0.0.1", "0.0.1") == true); |
| 2448 |
} |
| 2449 |
END_TEST |
| 2450 |
|
| 2451 |
START_TEST(strneq_test) |
| 2452 |
{ |
| 2453 |
litest_assert(strneq("", "", 1) == true); |
| 2454 |
litest_assert(strneq(NULL, NULL, 1) == true); |
| 2455 |
litest_assert(strneq("0.0.1", "", 6) == false); |
| 2456 |
litest_assert(strneq("foo", NULL, 5) == false); |
| 2457 |
litest_assert(strneq(NULL, "foo", 5) == false); |
| 2458 |
litest_assert(strneq("0.0.1", "0.0.1", 6) == true); |
| 2459 |
} |
| 2460 |
END_TEST |
| 2461 |
|
| 2462 |
START_TEST(basename_test) |
| 2463 |
{ |
| 2464 |
struct test { |
| 2465 |
const char *path; |
| 2466 |
const char *expected; |
| 2467 |
} tests[] = { |
| 2468 |
{ "a", "a" }, |
| 2469 |
{ "foo.c", "foo.c" }, |
| 2470 |
{ "foo", "foo" }, |
| 2471 |
{ "/path/to/foo.h", "foo.h" }, |
| 2472 |
{ "../bar.foo", "bar.foo" }, |
| 2473 |
{ "./bar.foo.baz", "bar.foo.baz" }, |
| 2474 |
{ "./", NULL }, |
| 2475 |
{ "/", NULL }, |
| 2476 |
{ "/bar/", NULL }, |
| 2477 |
{ "/bar", "bar" }, |
| 2478 |
{ "", NULL }, |
| 2479 |
}; |
| 2480 |
|
| 2481 |
ARRAY_FOR_EACH(tests, t) { |
| 2482 |
const char *result = safe_basename(t->path); |
| 2483 |
if (t->expected == NULL) |
| 2484 |
litest_assert(result == NULL); |
| 2485 |
else |
| 2486 |
litest_assert_str_eq(result, t->expected); |
| 2487 |
} |
| 2488 |
} |
| 2489 |
END_TEST |
| 2490 |
|
| 2491 |
START_TEST(trunkname_test) |
| 2492 |
{ |
| 2493 |
struct test { |
| 2494 |
const char *path; |
| 2495 |
const char *expected; |
| 2496 |
} tests[] = { |
| 2497 |
{ "foo.c", "foo" }, |
| 2498 |
{ "/path/to/foo.h", "foo" }, |
| 2499 |
{ "/path/to/foo", "foo" }, |
| 2500 |
{ "../bar.foo", "bar" }, |
| 2501 |
{ "./bar.foo.baz", "bar.foo" }, |
| 2502 |
{ "./", "" }, |
| 2503 |
{ "/", "" }, |
| 2504 |
{ "/bar/", "" }, |
| 2505 |
{ "/bar", "bar" }, |
| 2506 |
{ "", "" }, |
| 2507 |
}; |
| 2508 |
|
| 2509 |
ARRAY_FOR_EACH(tests, t) { |
| 2510 |
char *result = trunkname(t->path); |
| 2511 |
litest_assert_str_eq(result, t->expected); |
| 2512 |
free(result); |
| 2513 |
} |
| 2514 |
} |
| 2515 |
END_TEST |
| 2516 |
|
| 2517 |
START_TEST(absinfo_normalize_value_test) |
| 2518 |
{ |
| 2519 |
struct input_absinfo abs = { |
| 2520 |
.minimum = 0, |
| 2521 |
.maximum = 100, |
| 2522 |
}; |
| 2523 |
|
| 2524 |
litest_assert_double_eq(absinfo_normalize_value(&abs, -100), 0.0); |
| 2525 |
litest_assert_double_eq(absinfo_normalize_value(&abs, -1), 0.0); |
| 2526 |
litest_assert_double_eq(absinfo_normalize_value(&abs, 0), 0.0); |
| 2527 |
litest_assert_double_gt(absinfo_normalize_value(&abs, 1), 0.0); |
| 2528 |
litest_assert_double_lt(absinfo_normalize_value(&abs, 99), 1.0); |
| 2529 |
litest_assert_double_eq(absinfo_normalize_value(&abs, 100), 1.0); |
| 2530 |
litest_assert_double_eq(absinfo_normalize_value(&abs, 101), 1.0); |
| 2531 |
litest_assert_double_eq(absinfo_normalize_value(&abs, 200), 1.0); |
| 2532 |
|
| 2533 |
abs.minimum = -50; |
| 2534 |
abs.maximum = 50; |
| 2535 |
|
| 2536 |
litest_assert_double_eq(absinfo_normalize_value(&abs, -51), 0.0); |
| 2537 |
litest_assert_double_eq(absinfo_normalize_value(&abs, -50), 0.0); |
| 2538 |
litest_assert_double_eq(absinfo_normalize_value(&abs, 0), 0.5); |
| 2539 |
litest_assert_double_eq(absinfo_normalize_value(&abs, 50), 1.0); |
| 2540 |
litest_assert_double_eq(absinfo_normalize_value(&abs, 51), 1.0); |
| 2541 |
|
| 2542 |
abs.minimum = -50; |
| 2543 |
abs.maximum = 0; |
| 2544 |
|
| 2545 |
litest_assert_double_eq(absinfo_normalize_value(&abs, -51), 0.0); |
| 2546 |
litest_assert_double_eq(absinfo_normalize_value(&abs, -50), 0.0); |
| 2547 |
litest_assert_double_gt(absinfo_normalize_value(&abs, -49), 0.0); |
| 2548 |
litest_assert_double_lt(absinfo_normalize_value(&abs, -1), 1.0); |
| 2549 |
litest_assert_double_eq(absinfo_normalize_value(&abs, 0), 1.0); |
| 2550 |
} |
| 2551 |
END_TEST |
| 2552 |
|
| 2553 |
START_TEST(range_test) |
| 2554 |
{ |
| 2555 |
struct range incl = range_init_inclusive(1, 100); |
| 2556 |
litest_assert_int_eq(incl.lower, 1); |
| 2557 |
litest_assert_int_eq(incl.upper, 101); |
| 2558 |
|
| 2559 |
struct range excl = range_init_exclusive(1, 100); |
| 2560 |
litest_assert_int_eq(excl.lower, 1); |
| 2561 |
litest_assert_int_eq(excl.upper, 100); |
| 2562 |
|
| 2563 |
struct range zero = range_init_exclusive(0, 0); |
| 2564 |
litest_assert_int_eq(zero.lower, 0); |
| 2565 |
litest_assert_int_eq(zero.upper, 0); |
| 2566 |
|
| 2567 |
struct range empty = range_init_empty(); |
| 2568 |
litest_assert_int_eq(empty.lower, 0); |
| 2569 |
litest_assert_int_eq(empty.upper, -1); |
| 2570 |
|
| 2571 |
litest_assert(range_is_valid(&incl)); |
| 2572 |
litest_assert(range_is_valid(&excl)); |
| 2573 |
litest_assert(!range_is_valid(&zero)); |
| 2574 |
litest_assert(!range_is_valid(&empty)); |
| 2575 |
|
| 2576 |
int expected = 1; |
| 2577 |
int r = 0; |
| 2578 |
range_for_each(&incl, r) { |
| 2579 |
litest_assert_int_eq(r, expected); |
| 2580 |
expected++; |
| 2581 |
} |
| 2582 |
litest_assert_int_eq(r, 101); |
| 2583 |
} |
| 2584 |
END_TEST |
| 2585 |
|
| 2586 |
START_TEST(stringbuf_test) |
| 2587 |
{ |
| 2588 |
struct stringbuf buf; |
| 2589 |
struct stringbuf *b = &buf; |
| 2590 |
int rc; |
| 2591 |
|
| 2592 |
stringbuf_init(b); |
| 2593 |
litest_assert_int_eq(b->len, 0u); |
| 2594 |
|
| 2595 |
rc = stringbuf_append_string(b, "foo"); |
| 2596 |
litest_assert_neg_errno_success(rc); |
| 2597 |
rc = stringbuf_append_string(b, "bar"); |
| 2598 |
litest_assert_neg_errno_success(rc); |
| 2599 |
rc = stringbuf_append_string(b, "baz"); |
| 2600 |
litest_assert_neg_errno_success(rc); |
| 2601 |
litest_assert_str_eq(b->data, "foobarbaz"); |
| 2602 |
litest_assert_int_eq(b->len, strlen("foobarbaz")); |
| 2603 |
|
| 2604 |
rc = stringbuf_ensure_space(b, 500); |
| 2605 |
litest_assert_neg_errno_success(rc); |
| 2606 |
litest_assert_int_ge(b->sz, 500u); |
| 2607 |
|
| 2608 |
rc = stringbuf_ensure_size(b, 0); |
| 2609 |
litest_assert_neg_errno_success(rc); |
| 2610 |
rc = stringbuf_ensure_size(b, 1024); |
| 2611 |
litest_assert_neg_errno_success(rc); |
| 2612 |
litest_assert_int_ge(b->sz, 1024u); |
| 2613 |
|
| 2614 |
char *data = stringbuf_steal(b); |
| 2615 |
litest_assert_str_eq(data, "foobarbaz"); |
| 2616 |
litest_assert_int_eq(b->sz, 0u); |
| 2617 |
litest_assert_int_eq(b->len, 0u); |
| 2618 |
litest_assert_ptr_null(b->data); |
| 2619 |
free(data); |
| 2620 |
|
| 2621 |
const char *str = "1234567890"; |
| 2622 |
rc = stringbuf_append_string(b, str); |
| 2623 |
litest_assert_neg_errno_success(rc); |
| 2624 |
litest_assert_str_eq(b->data, str); |
| 2625 |
litest_assert_int_eq(b->len, 10u); |
| 2626 |
stringbuf_reset(b); |
| 2627 |
|
| 2628 |
/* intentional double-reset */ |
| 2629 |
stringbuf_reset(b); |
| 2630 |
|
| 2631 |
int pipefd[2]; |
| 2632 |
rc = pipe2(pipefd, O_CLOEXEC | O_NONBLOCK); |
| 2633 |
litest_assert_neg_errno_success(rc); |
| 2634 |
|
| 2635 |
str = "foo bar baz"; |
| 2636 |
char *compare = NULL; |
| 2637 |
for (int i = 0; i < 100; i++) { |
| 2638 |
rc = write(pipefd[1], str, strlen(str)); |
| 2639 |
litest_assert_neg_errno_success(rc); |
| 2640 |
|
| 2641 |
rc = stringbuf_append_from_fd(b, pipefd[0], 64); |
| 2642 |
litest_assert_neg_errno_success(rc); |
| 2643 |
|
| 2644 |
char *expected = strdup_printf("%s%s", compare ? compare : "", str); |
| 2645 |
litest_assert_ptr_notnull(expected); |
| 2646 |
litest_assert_str_eq(b->data, expected); |
| 2647 |
|
| 2648 |
free(compare); |
| 2649 |
compare = expected; |
| 2650 |
} |
| 2651 |
free(compare); |
| 2652 |
close(pipefd[0]); |
| 2653 |
close(pipefd[1]); |
| 2654 |
stringbuf_reset(b); |
| 2655 |
|
| 2656 |
rc = pipe2(pipefd, O_CLOEXEC | O_NONBLOCK); |
| 2657 |
litest_assert_neg_errno_success(rc); |
| 2658 |
|
| 2659 |
const size_t stride = 256; |
| 2660 |
const size_t maxsize = 4096; |
| 2661 |
|
| 2662 |
for (size_t i = 0; i < maxsize; i += stride) { |
| 2663 |
char buf[stride]; |
| 2664 |
memset(buf, i / stride, sizeof(buf)); |
| 2665 |
rc = write(pipefd[1], buf, sizeof(buf)); |
| 2666 |
litest_assert_neg_errno_success(rc); |
| 2667 |
} |
| 2668 |
|
| 2669 |
stringbuf_append_from_fd(b, pipefd[0], 0); |
| 2670 |
litest_assert_int_eq(b->len, maxsize); |
| 2671 |
litest_assert_int_ge(b->sz, maxsize); |
| 2672 |
|
| 2673 |
for (size_t i = 0; i < maxsize; i += stride) { |
| 2674 |
char buf[stride]; |
| 2675 |
memset(buf, i / stride, sizeof(buf)); |
| 2676 |
litest_assert_int_eq(memcmp(buf, b->data + i, sizeof(buf)), 0); |
| 2677 |
} |
| 2678 |
|
| 2679 |
close(pipefd[0]); |
| 2680 |
close(pipefd[1]); |
| 2681 |
stringbuf_reset(b); |
| 2682 |
} |
| 2683 |
END_TEST |
| 2684 |
|
| 2685 |
START_TEST(multivalue_test) |
| 2686 |
{ |
| 2687 |
{ |
| 2688 |
struct multivalue v = multivalue_new_string("test"); |
| 2689 |
litest_assert_int_eq(v.type, 's'); |
| 2690 |
litest_assert_str_eq(v.value.s, "test"); |
| 2691 |
|
| 2692 |
const char *s; |
| 2693 |
multivalue_extract_typed(&v, 's', &s); |
| 2694 |
litest_assert_str_eq(s, "test"); |
| 2695 |
litest_assert_ptr_eq(s, (const char *)v.value.s); |
| 2696 |
multivalue_extract(&v, &s); |
| 2697 |
litest_assert_str_eq(s, "test"); |
| 2698 |
litest_assert_ptr_eq(s, (const char *)v.value.s); |
| 2699 |
|
| 2700 |
struct multivalue copy = multivalue_copy(&v); |
| 2701 |
litest_assert_int_eq(copy.type, v.type); |
| 2702 |
litest_assert_str_eq(copy.value.s, v.value.s); |
| 2703 |
char *p1 = copy.value.s; |
| 2704 |
char *p2 = v.value.s; |
| 2705 |
litest_assert_ptr_ne(p1, p2); |
| 2706 |
|
| 2707 |
char *str = multivalue_as_str(&v); |
| 2708 |
litest_assert_str_eq(str, "test"); |
| 2709 |
free(str); |
| 2710 |
} |
| 2711 |
|
| 2712 |
{ |
| 2713 |
struct multivalue v = multivalue_new_char('x'); |
| 2714 |
litest_assert_int_eq(v.type, 'c'); |
| 2715 |
litest_assert_int_eq(v.value.c, 'x'); |
| 2716 |
|
| 2717 |
char c; |
| 2718 |
multivalue_extract_typed(&v, 'c', &c); |
| 2719 |
litest_assert_int_eq(c, 'x'); |
| 2720 |
multivalue_extract(&v, &c); |
| 2721 |
litest_assert_int_eq(c, 'x'); |
| 2722 |
|
| 2723 |
struct multivalue copy = multivalue_copy(&v); |
| 2724 |
litest_assert_int_eq(copy.type, v.type); |
| 2725 |
litest_assert_int_eq(copy.value.c, v.value.c); |
| 2726 |
|
| 2727 |
char *str = multivalue_as_str(&v); |
| 2728 |
litest_assert_str_eq(str, "x"); |
| 2729 |
free(str); |
| 2730 |
} |
| 2731 |
|
| 2732 |
{ |
| 2733 |
struct multivalue v = multivalue_new_u32(0x1234); |
| 2734 |
litest_assert_int_eq(v.type, 'u'); |
| 2735 |
litest_assert_int_eq(v.value.u, 0x1234u); |
| 2736 |
|
| 2737 |
uint32_t c; |
| 2738 |
multivalue_extract_typed(&v, 'u', &c); |
| 2739 |
litest_assert_int_eq(c, 0x1234u); |
| 2740 |
multivalue_extract(&v, &c); |
| 2741 |
litest_assert_int_eq(c, 0x1234u); |
| 2742 |
|
| 2743 |
struct multivalue copy = multivalue_copy(&v); |
| 2744 |
litest_assert_int_eq(copy.type, v.type); |
| 2745 |
litest_assert_int_eq(copy.value.u, v.value.u); |
| 2746 |
|
| 2747 |
char *str = multivalue_as_str(&v); |
| 2748 |
litest_assert_str_eq(str, "4660"); |
| 2749 |
free(str); |
| 2750 |
} |
| 2751 |
|
| 2752 |
{ |
| 2753 |
struct multivalue v = multivalue_new_i32(-123); |
| 2754 |
litest_assert_int_eq(v.type, 'i'); |
| 2755 |
litest_assert_int_eq(v.value.i, -123); |
| 2756 |
|
| 2757 |
int32_t c; |
| 2758 |
multivalue_extract_typed(&v, 'i', &c); |
| 2759 |
litest_assert_int_eq(c, -123); |
| 2760 |
multivalue_extract(&v, &c); |
| 2761 |
litest_assert_int_eq(c, -123); |
| 2762 |
|
| 2763 |
struct multivalue copy = multivalue_copy(&v); |
| 2764 |
litest_assert_int_eq(copy.type, v.type); |
| 2765 |
litest_assert_int_eq(copy.value.i, v.value.i); |
| 2766 |
|
| 2767 |
char *str = multivalue_as_str(&v); |
| 2768 |
litest_assert_str_eq(str, "-123"); |
| 2769 |
free(str); |
| 2770 |
} |
| 2771 |
|
| 2772 |
{ |
| 2773 |
struct multivalue v = multivalue_new_bool(true); |
| 2774 |
litest_assert_int_eq(v.type, 'b'); |
| 2775 |
litest_assert_int_eq(v.value.b, true); |
| 2776 |
|
| 2777 |
bool c; |
| 2778 |
multivalue_extract_typed(&v, 'b', &c); |
| 2779 |
litest_assert_int_eq(c, true); |
| 2780 |
multivalue_extract(&v, &c); |
| 2781 |
litest_assert_int_eq(c, true); |
| 2782 |
|
| 2783 |
struct multivalue copy = multivalue_copy(&v); |
| 2784 |
litest_assert_int_eq(copy.type, v.type); |
| 2785 |
litest_assert_int_eq(copy.value.b, v.value.b); |
| 2786 |
|
| 2787 |
char *str = multivalue_as_str(&v); |
| 2788 |
litest_assert_str_eq(str, "true"); |
| 2789 |
free(str); |
| 2790 |
} |
| 2791 |
|
| 2792 |
{ |
| 2793 |
struct multivalue v = multivalue_new_double(0.1234); |
| 2794 |
litest_assert_int_eq(v.type, 'd'); |
| 2795 |
litest_assert_double_eq(v.value.d, 0.1234); |
| 2796 |
|
| 2797 |
double c; |
| 2798 |
multivalue_extract_typed(&v, 'd', &c); |
| 2799 |
litest_assert_double_eq(c, 0.1234); |
| 2800 |
multivalue_extract(&v, &c); |
| 2801 |
litest_assert_double_eq(c, 0.1234); |
| 2802 |
|
| 2803 |
struct multivalue copy = multivalue_copy(&v); |
| 2804 |
litest_assert_int_eq(copy.type, v.type); |
| 2805 |
litest_assert_double_eq(copy.value.d, v.value.d); |
| 2806 |
|
| 2807 |
char *str = multivalue_as_str(&v); |
| 2808 |
litest_assert_str_eq(str, "0.123400"); |
| 2809 |
free(str); |
| 2810 |
} |
| 2811 |
} |
| 2812 |
END_TEST |
| 2813 |
|
| 2814 |
DECLARE_NEWTYPE(newint, int); |
| 2815 |
DECLARE_NEWTYPE(newdouble, double); |
| 2816 |
|
| 2817 |
START_TEST(newtype_test) |
| 2818 |
{ |
| 2819 |
{ |
| 2820 |
newint_t n1 = newint_from_int(1); |
| 2821 |
newint_t n2 = newint_from_int(2); |
| 2822 |
|
| 2823 |
litest_assert_int_eq(newint(n1), 1); |
| 2824 |
litest_assert_int_eq(newint_as_int(n1), 1); |
| 2825 |
litest_assert_int_eq(newint(n2), 2); |
| 2826 |
litest_assert_int_eq(newint_as_int(n2), 2); |
| 2827 |
|
| 2828 |
litest_assert_int_eq(newint_cmp(n1, n2), -1); |
| 2829 |
litest_assert_int_eq(newint_cmp(n1, n1), 0); |
| 2830 |
litest_assert_int_eq(newint_cmp(n2, n1), 1); |
| 2831 |
|
| 2832 |
newint_t copy = newint_copy(n1); |
| 2833 |
litest_assert_int_eq(newint_cmp(n1, copy), 0); |
| 2834 |
|
| 2835 |
newint_t min = newint_min(n1, n2); |
| 2836 |
newint_t max = newint_max(n1, n2); |
| 2837 |
litest_assert_int_eq(newint_cmp(min, n1), 0); |
| 2838 |
litest_assert_int_eq(newint_cmp(max, n2), 0); |
| 2839 |
|
| 2840 |
litest_assert(newint_gt(n1, 0)); |
| 2841 |
litest_assert(newint_eq(n1, 1)); |
| 2842 |
litest_assert(newint_ge(n1, 1)); |
| 2843 |
litest_assert(newint_le(n1, 1)); |
| 2844 |
litest_assert(newint_ne(n1, 2)); |
| 2845 |
litest_assert(newint_lt(n1, 2)); |
| 2846 |
|
| 2847 |
litest_assert(!newint_gt(n1, 1)); |
| 2848 |
litest_assert(!newint_eq(n1, 0)); |
| 2849 |
litest_assert(!newint_ge(n1, 2)); |
| 2850 |
litest_assert(!newint_le(n1, 0)); |
| 2851 |
litest_assert(!newint_ne(n1, 1)); |
| 2852 |
litest_assert(!newint_lt(n1, 1)); |
| 2853 |
} |
| 2854 |
{ |
| 2855 |
newdouble_t n1 = newdouble_from_double(1.2); |
| 2856 |
newdouble_t n2 = newdouble_from_double(2.3); |
| 2857 |
|
| 2858 |
litest_assert_double_eq(newdouble(n1), 1.2); |
| 2859 |
litest_assert_double_eq(newdouble_as_double(n1), 1.2); |
| 2860 |
litest_assert_double_eq(newdouble(n2), 2.3); |
| 2861 |
litest_assert_double_eq(newdouble_as_double(n2), 2.3); |
| 2862 |
|
| 2863 |
litest_assert_int_eq(newdouble_cmp(n1, n2), -1); |
| 2864 |
litest_assert_int_eq(newdouble_cmp(n1, n1), 0); |
| 2865 |
litest_assert_int_eq(newdouble_cmp(n2, n1), 1); |
| 2866 |
|
| 2867 |
newdouble_t copy = newdouble_copy(n1); |
| 2868 |
litest_assert_int_eq(newdouble_cmp(n1, copy), 0); |
| 2869 |
|
| 2870 |
newdouble_t min = newdouble_min(n1, n2); |
| 2871 |
newdouble_t max = newdouble_max(n1, n2); |
| 2872 |
litest_assert_int_eq(newdouble_cmp(min, n1), 0); |
| 2873 |
litest_assert_int_eq(newdouble_cmp(max, n2), 0); |
| 2874 |
|
| 2875 |
litest_assert(newdouble_gt(n1, 0.0)); |
| 2876 |
litest_assert(newdouble_eq(n1, 1.2)); |
| 2877 |
litest_assert(newdouble_ge(n1, 1.2)); |
| 2878 |
litest_assert(newdouble_le(n1, 1.2)); |
| 2879 |
litest_assert(newdouble_ne(n1, 2.3)); |
| 2880 |
litest_assert(newdouble_lt(n1, 2.3)); |
| 2881 |
|
| 2882 |
litest_assert(!newdouble_gt(n1, 1.2)); |
| 2883 |
litest_assert(!newdouble_eq(n1, 0.0)); |
| 2884 |
litest_assert(!newdouble_ge(n1, 2.3)); |
| 2885 |
litest_assert(!newdouble_le(n1, 0.0)); |
| 2886 |
litest_assert(!newdouble_ne(n1, 1.2)); |
| 2887 |
litest_assert(!newdouble_lt(n1, 1.2)); |
| 2888 |
} |
| 2889 |
} |
| 2890 |
END_TEST |
| 2891 |
|
| 2892 |
struct sunref {}; |
| 2893 |
struct sdestroy {}; |
| 2894 |
struct sfree {}; |
| 2895 |
|
| 2896 |
static void |
| 2897 |
sunref_unref(struct sunref *s) |
| 2898 |
{ |
| 2899 |
free(s); |
| 2900 |
} |
| 2901 |
static void |
| 2902 |
sdestroy_destroy(struct sdestroy *s) |
| 2903 |
{ |
| 2904 |
free(s); |
| 2905 |
} |
| 2906 |
static void |
| 2907 |
sfree_free(struct sfree *s) |
| 2908 |
{ |
| 2909 |
free(s); |
| 2910 |
} |
| 2911 |
|
| 2912 |
DEFINE_UNREF_CLEANUP_FUNC(sunref); |
| 2913 |
DEFINE_DESTROY_CLEANUP_FUNC(sdestroy); |
| 2914 |
DEFINE_FREE_CLEANUP_FUNC(sfree); |
| 2915 |
|
| 2916 |
START_TEST(attribute_cleanup) |
| 2917 |
{ |
| 2918 |
/* These tests will likely only show up in valgrind, |
| 2919 |
* the various asserts are just to shut up the compiler |
| 2920 |
* about unused variables |
| 2921 |
*/ |
| 2922 |
{ |
| 2923 |
_autofree_ char *autofree = zalloc(64); |
| 2924 |
litest_assert(autofree); |
| 2925 |
} |
| 2926 |
{ |
| 2927 |
_autofree_ char *stolen = zalloc(64); |
| 2928 |
free(steal(&stolen)); |
| 2929 |
} |
| 2930 |
{ |
| 2931 |
_autoclose_ int fd = open("/proc/self/cmdline", O_RDONLY); |
| 2932 |
litest_assert_int_ge(fd, 0); |
| 2933 |
|
| 2934 |
_autoclose_ int badfd = -1; |
| 2935 |
litest_assert_int_eq(badfd, -1); |
| 2936 |
|
| 2937 |
_autoclose_ int stealfd = open("/proc/self/cmdline", O_RDONLY); |
| 2938 |
steal_fd(&stealfd); |
| 2939 |
litest_assert_int_eq(stealfd, -1); |
| 2940 |
} |
| 2941 |
{ |
| 2942 |
_autostrvfree_ char **strv = zalloc(3 * sizeof(*strv)); |
| 2943 |
for (int i = 0; i < 2; i++) { |
| 2944 |
strv[i] = strdup_printf("element %d", i); |
| 2945 |
} |
| 2946 |
|
| 2947 |
_autostrvfree_ char **badstrv = NULL; |
| 2948 |
litest_assert_ptr_null(badstrv); |
| 2949 |
} |
| 2950 |
{ |
| 2951 |
_autofclose_ FILE *fp = fopen("/proc/self/cmdline", "r"); |
| 2952 |
litest_assert_ptr_notnull(fp); |
| 2953 |
|
| 2954 |
_autofclose_ FILE *badfd = NULL; |
| 2955 |
litest_assert_ptr_null(badfd); |
| 2956 |
} |
| 2957 |
{ |
| 2958 |
_unref_(sunref) *s = zalloc(sizeof(*s)); |
| 2959 |
} |
| 2960 |
{ |
| 2961 |
_destroy_(sdestroy) *s = zalloc(sizeof(*s)); |
| 2962 |
} |
| 2963 |
{ |
| 2964 |
_free_(sfree) *s = zalloc(sizeof(*s)); |
| 2965 |
} |
| 2966 |
} |
| 2967 |
END_TEST |
| 2968 |
|
| 2969 |
START_TEST(macros_expand) |
| 2970 |
{ |
| 2971 |
#define _A1(_1) _1, #_1 |
| 2972 |
#define _A2(_1, _2) _1, _2 |
| 2973 |
#define A(...) _VARIABLE_MACRO(_A, __VA_ARGS__) |
| 2974 |
char buf[64]; |
| 2975 |
snprintf(buf, sizeof(buf), "%d:%s", A(0)); |
| 2976 |
litest_assert_str_eq(buf, "0:0"); |
| 2977 |
snprintf(buf, sizeof(buf), "%d:%s", A(100)); |
| 2978 |
litest_assert_str_eq(buf, "100:100"); |
| 2979 |
snprintf(buf, sizeof(buf), "%d:%s", A(100, "hundred")); |
| 2980 |
litest_assert_str_eq(buf, "100:hundred"); |
| 2981 |
#undef _A1 |
| 2982 |
#undef _A2 |
| 2983 |
#undef A |
| 2984 |
} |
| 2985 |
END_TEST |
| 2986 |
|
| 2987 |
START_TEST(evdev_frames) |
| 2988 |
{ |
| 2989 |
#define U(u_) evdev_usage_from_uint32_t(u_) |
| 2990 |
{ |
| 2991 |
evdev_frame_unref(NULL); /* unref on NULL is permitted */ |
| 2992 |
} |
| 2993 |
{ |
| 2994 |
_unref_(evdev_frame) *frame = evdev_frame_new(3); |
| 2995 |
litest_assert_int_eq(evdev_frame_get_count(frame), 1U); /* SYN_REPORT */ |
| 2996 |
|
| 2997 |
litest_assert_ptr_eq(evdev_frame_ref(frame), frame); |
| 2998 |
litest_assert_ptr_eq(evdev_frame_unref(frame), NULL); |
| 2999 |
} |
| 3000 |
{ |
| 3001 |
_unref_(evdev_frame) *frame = evdev_frame_new(3); |
| 3002 |
struct evdev_event toobig[] = { |
| 3003 |
{ |
| 3004 |
.usage = U(EVDEV_ABS_X), |
| 3005 |
.value = 1, |
| 3006 |
}, |
| 3007 |
{ |
| 3008 |
.usage = U(EVDEV_ABS_Y), |
| 3009 |
.value = 2, |
| 3010 |
}, |
| 3011 |
{ |
| 3012 |
.usage = U(EVDEV_ABS_Z), |
| 3013 |
.value = 3, |
| 3014 |
}, |
| 3015 |
{ |
| 3016 |
.usage = U(EVDEV_SYN_REPORT), |
| 3017 |
.value = 0, |
| 3018 |
}, |
| 3019 |
}; |
| 3020 |
|
| 3021 |
int rc = evdev_frame_set(frame, toobig, ARRAY_LENGTH(toobig)); |
| 3022 |
litest_assert_int_eq(rc, -ENOMEM); |
| 3023 |
} |
| 3024 |
{ |
| 3025 |
struct evdev_event events[] = { |
| 3026 |
{ |
| 3027 |
.usage = U(EVDEV_ABS_X), |
| 3028 |
.value = 1, |
| 3029 |
}, |
| 3030 |
{ |
| 3031 |
.usage = U(EVDEV_ABS_Y), |
| 3032 |
.value = 2, |
| 3033 |
}, |
| 3034 |
{ |
| 3035 |
.usage = U(EVDEV_SYN_REPORT), |
| 3036 |
.value = 0, |
| 3037 |
}, |
| 3038 |
}; |
| 3039 |
|
| 3040 |
_unref_(evdev_frame) *frame = evdev_frame_new(3); |
| 3041 |
int rc = evdev_frame_set(frame, events, ARRAY_LENGTH(events)); |
| 3042 |
litest_assert_neg_errno_success(rc); |
| 3043 |
litest_assert_int_eq(evdev_frame_get_count(frame), |
| 3044 |
ARRAY_LENGTH(events)); |
| 3045 |
litest_assert_int_eq(frame->max_size, ARRAY_LENGTH(events)); |
| 3046 |
|
| 3047 |
size_t nevents; |
| 3048 |
rc = memcmp(evdev_frame_get_events(frame, &nevents), |
| 3049 |
events, |
| 3050 |
sizeof(events)); |
| 3051 |
litest_assert_int_eq(rc, 0); |
| 3052 |
litest_assert_int_eq(nevents, ARRAY_LENGTH(events)); |
| 3053 |
|
| 3054 |
/* Already full, can't append */ |
| 3055 |
rc = evdev_frame_append(frame, events, 1); |
| 3056 |
litest_assert_int_eq(rc, -ENOMEM); |
| 3057 |
} |
| 3058 |
{ |
| 3059 |
struct evdev_event events[] = { |
| 3060 |
{ |
| 3061 |
.usage = U(EVDEV_ABS_X), |
| 3062 |
.value = 1, |
| 3063 |
}, |
| 3064 |
{ |
| 3065 |
.usage = U(EVDEV_ABS_Y), |
| 3066 |
.value = 2, |
| 3067 |
}, |
| 3068 |
{ |
| 3069 |
.usage = U(EVDEV_SYN_REPORT), |
| 3070 |
.value = 0, |
| 3071 |
}, |
| 3072 |
}; |
| 3073 |
|
| 3074 |
_unref_(evdev_frame) *frame = evdev_frame_new(3); |
| 3075 |
int rc = evdev_frame_set(frame, events, 1); |
| 3076 |
litest_assert_neg_errno_success(rc); |
| 3077 |
litest_assert_int_eq(evdev_frame_get_count(frame), |
| 3078 |
2U); /* we appended SYN_REPORT */ |
| 3079 |
rc = evdev_frame_append(frame, events + 1, 1); |
| 3080 |
litest_assert_neg_errno_success(rc); |
| 3081 |
litest_assert_int_eq(evdev_frame_get_count(frame), |
| 3082 |
3U); /* we appended SYN_REPORT */ |
| 3083 |
rc = evdev_frame_append(frame, events + 2, 1); |
| 3084 |
litest_assert_neg_errno_success(rc); |
| 3085 |
litest_assert_int_eq(evdev_frame_get_count(frame), |
| 3086 |
3U); /* SYN_REPORT already there */ |
| 3087 |
} |
| 3088 |
{ |
| 3089 |
struct evdev_event interrupted[] = { |
| 3090 |
{ |
| 3091 |
.usage = U(EVDEV_ABS_X), |
| 3092 |
.value = 1, |
| 3093 |
}, |
| 3094 |
{ |
| 3095 |
.usage = U(EVDEV_ABS_Y), |
| 3096 |
.value = 2, |
| 3097 |
}, |
| 3098 |
{ |
| 3099 |
.usage = U(EVDEV_SYN_REPORT), |
| 3100 |
.value = 0, |
| 3101 |
}, |
| 3102 |
{ |
| 3103 |
.usage = U(EVDEV_ABS_RX), |
| 3104 |
.value = 1, |
| 3105 |
}, |
| 3106 |
{ |
| 3107 |
.usage = U(EVDEV_ABS_RY), |
| 3108 |
.value = 2, |
| 3109 |
}, |
| 3110 |
{ |
| 3111 |
.usage = U(EVDEV_SYN_REPORT), |
| 3112 |
.value = 0, |
| 3113 |
}, |
| 3114 |
}; |
| 3115 |
|
| 3116 |
_unref_(evdev_frame) *frame = evdev_frame_new(5); |
| 3117 |
int rc = evdev_frame_set(frame, interrupted, ARRAY_LENGTH(interrupted)); |
| 3118 |
litest_assert_neg_errno_success(rc); |
| 3119 |
litest_assert_int_eq(evdev_frame_get_count(frame), 3U); |
| 3120 |
|
| 3121 |
rc = evdev_frame_set(frame, &interrupted[2], 1); |
| 3122 |
litest_assert_neg_errno_success(rc); |
| 3123 |
litest_assert_int_eq(evdev_frame_get_count(frame), 1U); |
| 3124 |
|
| 3125 |
rc = evdev_frame_set(frame, |
| 3126 |
&interrupted[1], |
| 3127 |
ARRAY_LENGTH(interrupted) - 1); |
| 3128 |
litest_assert_neg_errno_success(rc); |
| 3129 |
litest_assert_int_eq(evdev_frame_get_count(frame), 2U); |
| 3130 |
|
| 3131 |
/* We never appended a timestamp */ |
| 3132 |
litest_assert(usec_cmp(evdev_frame_get_time(frame), |
| 3133 |
usec_from_uint64_t(0)) == 0); |
| 3134 |
} |
| 3135 |
{ |
| 3136 |
struct evdev_event events[] = { |
| 3137 |
{ |
| 3138 |
.usage = U(EVDEV_ABS_X), |
| 3139 |
.value = 1, |
| 3140 |
}, |
| 3141 |
{ |
| 3142 |
.usage = U(EVDEV_ABS_Y), |
| 3143 |
.value = 2, |
| 3144 |
}, |
| 3145 |
{ |
| 3146 |
.usage = U(EVDEV_SYN_REPORT), |
| 3147 |
.value = 0, |
| 3148 |
}, |
| 3149 |
}; |
| 3150 |
|
| 3151 |
_unref_(evdev_frame) *frame = evdev_frame_new(3); |
| 3152 |
int rc = evdev_frame_append_one(frame, U(EVDEV_ABS_X), 1); |
| 3153 |
litest_assert_neg_errno_success(rc); |
| 3154 |
rc = evdev_frame_append_one(frame, U(EVDEV_ABS_Y), 2); |
| 3155 |
litest_assert_neg_errno_success(rc); |
| 3156 |
rc = evdev_frame_append_one(frame, U(EV_SYN), 0); |
| 3157 |
litest_assert_neg_errno_success(rc); |
| 3158 |
|
| 3159 |
litest_assert_int_eq(evdev_frame_get_count(frame), |
| 3160 |
ARRAY_LENGTH(events)); |
| 3161 |
litest_assert_int_eq(frame->max_size, ARRAY_LENGTH(events)); |
| 3162 |
|
| 3163 |
size_t nevents; |
| 3164 |
rc = memcmp(evdev_frame_get_events(frame, &nevents), |
| 3165 |
events, |
| 3166 |
sizeof(events)); |
| 3167 |
litest_assert_int_eq(rc, 0); |
| 3168 |
litest_assert_int_eq(nevents, ARRAY_LENGTH(events)); |
| 3169 |
|
| 3170 |
/* Already full, can't append */ |
| 3171 |
rc = evdev_frame_append_one(frame, U(EVDEV_ABS_Z), 1); |
| 3172 |
litest_assert_int_eq(rc, -ENOMEM); |
| 3173 |
|
| 3174 |
/* Appending SYN_REPORT is a noop */ |
| 3175 |
rc = evdev_frame_append_one(frame, U(EVDEV_SYN_REPORT), 0); |
| 3176 |
litest_assert_neg_errno_success(rc); |
| 3177 |
litest_assert_int_eq(evdev_frame_get_count(frame), |
| 3178 |
ARRAY_LENGTH(events)); |
| 3179 |
litest_assert_int_eq(frame->max_size, ARRAY_LENGTH(events)); |
| 3180 |
} |
| 3181 |
{ |
| 3182 |
struct evdev_event events[] = { |
| 3183 |
{ |
| 3184 |
.usage = U(EVDEV_ABS_X), |
| 3185 |
.value = 1, |
| 3186 |
}, |
| 3187 |
{ |
| 3188 |
.usage = U(EVDEV_ABS_Y), |
| 3189 |
.value = 2, |
| 3190 |
}, |
| 3191 |
{ |
| 3192 |
.usage = U(EVDEV_SYN_REPORT), |
| 3193 |
.value = 1, |
| 3194 |
}, |
| 3195 |
}; |
| 3196 |
|
| 3197 |
_unref_(evdev_frame) *frame = evdev_frame_new(3); |
| 3198 |
int rc = evdev_frame_append(frame, events, 3); |
| 3199 |
litest_assert_neg_errno_success(rc); |
| 3200 |
|
| 3201 |
litest_assert_int_eq(evdev_frame_get_count(frame), |
| 3202 |
ARRAY_LENGTH(events)); |
| 3203 |
litest_assert_int_eq(frame->max_size, ARRAY_LENGTH(events)); |
| 3204 |
|
| 3205 |
size_t nevents; |
| 3206 |
rc = memcmp(evdev_frame_get_events(frame, &nevents), |
| 3207 |
events, |
| 3208 |
sizeof(events)); |
| 3209 |
litest_assert_int_eq(rc, 0); |
| 3210 |
litest_assert_int_eq(nevents, ARRAY_LENGTH(events)); |
| 3211 |
|
| 3212 |
for (int v = 0; v < 2; v++) { |
| 3213 |
/* Appending SYN_REPORT changes the value to zero */ |
| 3214 |
rc = evdev_frame_append_one(frame, U(EVDEV_SYN_REPORT), v); |
| 3215 |
litest_assert_neg_errno_success(rc); |
| 3216 |
litest_assert_int_eq(evdev_frame_get_count(frame), |
| 3217 |
ARRAY_LENGTH(events)); |
| 3218 |
struct evdev_event *evs = |
| 3219 |
evdev_frame_get_events(frame, &nevents); |
| 3220 |
litest_assert(evdev_usage_eq(evs[2].usage, EVDEV_SYN_REPORT)); |
| 3221 |
litest_assert_int_eq(evs[2].value, v); |
| 3222 |
} |
| 3223 |
} |
| 3224 |
} |
| 3225 |
END_TEST |
| 3226 |
|
| 3227 |
START_TEST(infmask_test) |
| 3228 |
{ |
| 3229 |
/* Test empty mask */ |
| 3230 |
infmask_t empty = infmask_new(); |
| 3231 |
litest_assert(infmask_is_empty(&empty)); |
| 3232 |
litest_assert(!infmask_bit_is_set(&empty, 0)); |
| 3233 |
litest_assert(!infmask_bit_is_set(&empty, 100)); |
| 3234 |
infmask_reset(&empty); |
| 3235 |
|
| 3236 |
/* Test single bit operations */ |
| 3237 |
infmask_t single = infmask_new(); |
| 3238 |
litest_assert(!infmask_set_bit(&single, 5)); |
| 3239 |
litest_assert(infmask_bit_is_set(&single, 5)); |
| 3240 |
litest_assert(!infmask_bit_is_set(&single, 4)); |
| 3241 |
litest_assert(!infmask_bit_is_set(&single, 6)); |
| 3242 |
litest_assert(!infmask_is_empty(&single)); |
| 3243 |
litest_assert(infmask_clear_bit(&single, 5)); |
| 3244 |
litest_assert(!infmask_bit_is_set(&single, 5)); |
| 3245 |
litest_assert(infmask_is_empty(&single)); |
| 3246 |
infmask_reset(&single); |
| 3247 |
|
| 3248 |
/* Test from_bit constructor */ |
| 3249 |
infmask_t from_bit = infmask_from_bit(7); |
| 3250 |
litest_assert(infmask_bit_is_set(&from_bit, 7)); |
| 3251 |
litest_assert(!infmask_bit_is_set(&from_bit, 6)); |
| 3252 |
litest_assert(!infmask_bit_is_set(&from_bit, 8)); |
| 3253 |
infmask_reset(&from_bit); |
| 3254 |
|
| 3255 |
/* Test from_bits constructor */ |
| 3256 |
infmask_t from_bits = infmask_from_bits(1, 3, 5); |
| 3257 |
litest_assert(infmask_bit_is_set(&from_bits, 1)); |
| 3258 |
litest_assert(!infmask_bit_is_set(&from_bits, 2)); |
| 3259 |
litest_assert(infmask_bit_is_set(&from_bits, 3)); |
| 3260 |
litest_assert(!infmask_bit_is_set(&from_bits, 4)); |
| 3261 |
litest_assert(infmask_bit_is_set(&from_bits, 5)); |
| 3262 |
infmask_reset(&from_bits); |
| 3263 |
|
| 3264 |
/* Test high bit operations */ |
| 3265 |
infmask_t high = infmask_new(); |
| 3266 |
litest_assert(!infmask_set_bit(&high, 100)); |
| 3267 |
litest_assert(infmask_bit_is_set(&high, 100)); |
| 3268 |
litest_assert(!infmask_bit_is_set(&high, 99)); |
| 3269 |
litest_assert(!infmask_bit_is_set(&high, 101)); |
| 3270 |
litest_assert(infmask_clear_bit(&high, 100)); |
| 3271 |
litest_assert(!infmask_bit_is_set(&high, 100)); |
| 3272 |
infmask_reset(&high); |
| 3273 |
|
| 3274 |
/* Test any/all operations */ |
| 3275 |
infmask_t mask1 = infmask_from_bits(1, 2, 3); |
| 3276 |
infmask_t mask2 = infmask_from_bits(2, 3, 4); |
| 3277 |
infmask_t mask3 = infmask_from_bits(2, 3); |
| 3278 |
|
| 3279 |
litest_assert(infmask_any(&mask1, &mask2)); |
| 3280 |
litest_assert(!infmask_all(&mask1, &mask2)); |
| 3281 |
litest_assert(infmask_all(&mask1, &mask3)); |
| 3282 |
litest_assert(infmask_any(&mask1, &mask3)); |
| 3283 |
|
| 3284 |
infmask_reset(&mask1); |
| 3285 |
infmask_reset(&mask2); |
| 3286 |
infmask_reset(&mask3); |
| 3287 |
|
| 3288 |
/* Test merge operation */ |
| 3289 |
infmask_t merge1 = infmask_from_bits(1, 2); |
| 3290 |
infmask_t merge2 = infmask_from_bits(2, 3); |
| 3291 |
litest_assert(!infmask_merge(&merge1, &merge2)); |
| 3292 |
litest_assert(infmask_bit_is_set(&merge1, 1)); |
| 3293 |
litest_assert(infmask_bit_is_set(&merge1, 2)); |
| 3294 |
litest_assert(infmask_bit_is_set(&merge1, 3)); |
| 3295 |
infmask_reset(&merge1); |
| 3296 |
infmask_reset(&merge2); |
| 3297 |
|
| 3298 |
/* Test clear operation */ |
| 3299 |
infmask_t clear1 = infmask_from_bits(1, 2, 3); |
| 3300 |
infmask_t clear2 = infmask_from_bits(2, 3); |
| 3301 |
litest_assert(infmask_clear(&clear1, &clear2)); |
| 3302 |
litest_assert(infmask_bit_is_set(&clear1, 1)); |
| 3303 |
litest_assert(!infmask_bit_is_set(&clear1, 2)); |
| 3304 |
litest_assert(!infmask_bit_is_set(&clear1, 3)); |
| 3305 |
infmask_reset(&clear1); |
| 3306 |
infmask_reset(&clear2); |
| 3307 |
|
| 3308 |
/* Test growing behavior */ |
| 3309 |
infmask_t grow = infmask_new(); |
| 3310 |
litest_assert(!infmask_set_bit(&grow, 5)); |
| 3311 |
litest_assert(grow.nmasks == 1); |
| 3312 |
litest_assert(!infmask_set_bit(&grow, 35)); |
| 3313 |
litest_assert(grow.nmasks == 2); |
| 3314 |
litest_assert(!infmask_set_bit(&grow, 65)); |
| 3315 |
litest_assert(grow.nmasks == 3); |
| 3316 |
litest_assert(infmask_bit_is_set(&grow, 5)); |
| 3317 |
litest_assert(infmask_bit_is_set(&grow, 35)); |
| 3318 |
litest_assert(infmask_bit_is_set(&grow, 65)); |
| 3319 |
infmask_reset(&grow); |
| 3320 |
} |
| 3321 |
END_TEST |
| 3322 |
|
| 3323 |
START_TEST(evdev_mask_test) |
| 3324 |
{ |
| 3325 |
_destroy_(evdev_mask) *mask = evdev_mask_new(); |
| 3326 |
|
| 3327 |
evdev_mask_reset(mask); |
| 3328 |
|
| 3329 |
litest_assert(bitmask_is_empty(mask->ev)); |
| 3330 |
litest_assert(bitmask_is_empty(mask->rel)); |
| 3331 |
litest_assert(bitmask_is_empty(mask->sw)); |
| 3332 |
litest_assert(infmask_is_empty(&mask->key)); |
| 3333 |
litest_assert(infmask_is_empty(&mask->btn)); |
| 3334 |
litest_assert(infmask_is_empty(&mask->abs)); |
| 3335 |
|
| 3336 |
evdev_mask_set_enum(mask, EVDEV_BTN_TOOL_PEN); |
| 3337 |
evdev_mask_set_enum(mask, EVDEV_BTN_TOOL_AIRBRUSH); |
| 3338 |
|
| 3339 |
litest_assert(bitmask_bit_is_set(mask->ev, EV_KEY)); |
| 3340 |
|
| 3341 |
/* Verify these are in btn, not key */ |
| 3342 |
litest_assert(!infmask_is_empty(&mask->btn)); |
| 3343 |
litest_assert(infmask_is_empty(&mask->key)); |
| 3344 |
|
| 3345 |
litest_assert(evdev_mask_is_set(mask, evdev_usage_from(EVDEV_BTN_TOOL_PEN))); |
| 3346 |
litest_assert( |
| 3347 |
!evdev_mask_is_set(mask, evdev_usage_from(EVDEV_BTN_TOOL_RUBBER))); |
| 3348 |
litest_assert( |
| 3349 |
evdev_mask_is_set(mask, evdev_usage_from(EVDEV_BTN_TOOL_AIRBRUSH))); |
| 3350 |
|
| 3351 |
/* Test regular key (should go into key field) */ |
| 3352 |
evdev_mask_set_enum(mask, EVDEV_KEY_ESC); |
| 3353 |
litest_assert(!infmask_is_empty(&mask->key)); |
| 3354 |
litest_assert(evdev_mask_is_set(mask, evdev_usage_from(EVDEV_KEY_ESC))); |
| 3355 |
|
| 3356 |
evdev_mask_set_enum(mask, EVDEV_REL_X); |
| 3357 |
litest_assert(bitmask_bit_is_set(mask->ev, EV_REL)); |
| 3358 |
litest_assert(bitmask_bit_is_set(mask->rel, REL_X)); |
| 3359 |
litest_assert(evdev_mask_is_set(mask, evdev_usage_from(EVDEV_REL_X))); |
| 3360 |
|
| 3361 |
evdev_mask_set_enum(mask, EVDEV_ABS_X); |
| 3362 |
litest_assert(bitmask_bit_is_set(mask->ev, EV_ABS)); |
| 3363 |
litest_assert(!infmask_is_empty(&mask->abs)); |
| 3364 |
litest_assert(evdev_mask_is_set(mask, evdev_usage_from(EVDEV_ABS_X))); |
| 3365 |
} |
| 3366 |
END_TEST |
| 3367 |
|
| 3368 |
int |
| 3369 |
main(void) |
| 3370 |
{ |
| 3371 |
struct litest_runner *runner = litest_runner_new(); |
| 3372 |
|
| 3373 |
/* not worth forking the tests here */ |
| 3374 |
litest_runner_set_num_parallel(runner, 0); |
| 3375 |
|
| 3376 |
#define ADD_TEST(func_) do { \ |
| 3377 |
struct litest_runner_test_description tdesc = { \ |
| 3378 |
.func = func_, \ |
| 3379 |
};\ |
| 3380 |
snprintf(tdesc.name, sizeof(tdesc.name), # func_); \ |
| 3381 |
litest_runner_add_test(runner, &tdesc); \ |
| 3382 |
} while(0) |
| 3383 |
|
| 3384 |
ADD_TEST(auto_test); |
| 3385 |
ADD_TEST(mkdir_p_test); |
| 3386 |
ADD_TEST(rmdir_r_test); |
| 3387 |
ADD_TEST(tmpdir_test); |
| 3388 |
ADD_TEST(find_files_test); |
| 3389 |
|
| 3390 |
ADD_TEST(array_for_each); |
| 3391 |
|
| 3392 |
ADD_TEST(bitfield_helpers); |
| 3393 |
ADD_TEST(bitmask_test); |
| 3394 |
ADD_TEST(matrix_helpers); |
| 3395 |
ADD_TEST(ratelimit_helpers); |
| 3396 |
ADD_TEST(dpi_parser); |
| 3397 |
ADD_TEST(wheel_click_parser); |
| 3398 |
ADD_TEST(wheel_click_count_parser); |
| 3399 |
ADD_TEST(dimension_prop_parser); |
| 3400 |
ADD_TEST(reliability_prop_parser); |
| 3401 |
ADD_TEST(calibration_prop_parser); |
| 3402 |
ADD_TEST(range_prop_parser); |
| 3403 |
ADD_TEST(boolean_prop_parser); |
| 3404 |
ADD_TEST(evcode_prop_parser); |
| 3405 |
ADD_TEST(input_prop_parser); |
| 3406 |
ADD_TEST(evdev_abs_parser); |
| 3407 |
ADD_TEST(safe_atoi_test); |
| 3408 |
ADD_TEST(safe_atoi_base_16_test); |
| 3409 |
ADD_TEST(safe_atoi_base_8_test); |
| 3410 |
ADD_TEST(safe_atou_test); |
| 3411 |
ADD_TEST(safe_atou_base_16_test); |
| 3412 |
ADD_TEST(safe_atou_base_8_test); |
| 3413 |
ADD_TEST(safe_atou64_test); |
| 3414 |
ADD_TEST(safe_atod_test); |
| 3415 |
ADD_TEST(strsplit_test); |
| 3416 |
ADD_TEST(strv_for_each_test); |
| 3417 |
ADD_TEST(strv_append_test); |
| 3418 |
ADD_TEST(strv_find_test); |
| 3419 |
ADD_TEST(strv_find_substring_test); |
| 3420 |
ADD_TEST(double_array_from_string_test); |
| 3421 |
ADD_TEST(strargv_test); |
| 3422 |
ADD_TEST(kvsplit_double_test); |
| 3423 |
ADD_TEST(strjoin_test); |
| 3424 |
ADD_TEST(strstrip_test); |
| 3425 |
ADD_TEST(strendswith_test); |
| 3426 |
ADD_TEST(strstartswith_test); |
| 3427 |
ADD_TEST(strsanitize_test); |
| 3428 |
ADD_TEST(time_conversion); |
| 3429 |
ADD_TEST(human_time); |
| 3430 |
|
| 3431 |
ADD_TEST(list_test_insert); |
| 3432 |
ADD_TEST(list_test_append); |
| 3433 |
ADD_TEST(list_test_foreach); |
| 3434 |
ADD_TEST(list_test_first_last); |
| 3435 |
ADD_TEST(list_test_chain); |
| 3436 |
ADD_TEST(strverscmp_test); |
| 3437 |
ADD_TEST(streq_test); |
| 3438 |
ADD_TEST(strneq_test); |
| 3439 |
ADD_TEST(trunkname_test); |
| 3440 |
ADD_TEST(basename_test); |
| 3441 |
|
| 3442 |
ADD_TEST(absinfo_normalize_value_test); |
| 3443 |
|
| 3444 |
ADD_TEST(range_test); |
| 3445 |
ADD_TEST(stringbuf_test); |
| 3446 |
ADD_TEST(multivalue_test); |
| 3447 |
|
| 3448 |
ADD_TEST(newtype_test); |
| 3449 |
ADD_TEST(attribute_cleanup); |
| 3450 |
ADD_TEST(macros_expand); |
| 3451 |
|
| 3452 |
ADD_TEST(evdev_frames); |
| 3453 |
|
| 3454 |
ADD_TEST(infmask_test); |
| 3455 |
|
| 3456 |
ADD_TEST(evdev_mask_test); |
| 3457 |
|
| 3458 |
enum litest_runner_result result = litest_runner_run_tests(runner); |
| 3459 |
litest_runner_destroy(runner); |
| 3460 |
|
| 3461 |
if (result == LITEST_SKIP) |
| 3462 |
return 77; |
| 3463 |
|
| 3464 |
return result - LITEST_PASS; |
| 3465 |
} |