diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:49:52 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:49:52 +0000 |
commit | 55944e5e40b1be2afc4855d8d2baf4b73d1876b5 (patch) | |
tree | 33f869f55a1b149e9b7c2b7e201867ca5dd52992 /src/test/test-bitmap.c | |
parent | Initial commit. (diff) | |
download | systemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.tar.xz systemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.zip |
Adding upstream version 255.4.upstream/255.4
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/test-bitmap.c')
-rw-r--r-- | src/test/test-bitmap.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/test/test-bitmap.c b/src/test/test-bitmap.c new file mode 100644 index 0000000..8acf833 --- /dev/null +++ b/src/test/test-bitmap.c @@ -0,0 +1,116 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "bitmap.h" +#include "tests.h" + +int main(int argc, const char *argv[]) { + _cleanup_bitmap_free_ Bitmap *b = NULL, *b2 = NULL; + unsigned n = UINT_MAX, i = 0; + + test_setup_logging(LOG_DEBUG); + + b = bitmap_new(); + assert_se(b); + + assert_se(bitmap_ensure_allocated(&b) == 0); + b = bitmap_free(b); + assert_se(bitmap_ensure_allocated(&b) == 0); + + assert_se(bitmap_isset(b, 0) == false); + assert_se(bitmap_isset(b, 1) == false); + assert_se(bitmap_isset(b, 256) == false); + assert_se(bitmap_isclear(b) == true); + + assert_se(bitmap_set(b, 0) == 0); + assert_se(bitmap_isset(b, 0) == true); + assert_se(bitmap_isclear(b) == false); + bitmap_unset(b, 0); + assert_se(bitmap_isset(b, 0) == false); + assert_se(bitmap_isclear(b) == true); + + assert_se(bitmap_set(b, 1) == 0); + assert_se(bitmap_isset(b, 1) == true); + assert_se(bitmap_isclear(b) == false); + bitmap_unset(b, 1); + assert_se(bitmap_isset(b, 1) == false); + assert_se(bitmap_isclear(b) == true); + + assert_se(bitmap_set(b, 256) == 0); + assert_se(bitmap_isset(b, 256) == true); + assert_se(bitmap_isclear(b) == false); + bitmap_unset(b, 256); + assert_se(bitmap_isset(b, 256) == false); + assert_se(bitmap_isclear(b) == true); + + assert_se(bitmap_set(b, 32) == 0); + bitmap_unset(b, 0); + assert_se(bitmap_isset(b, 32) == true); + bitmap_unset(b, 32); + + BITMAP_FOREACH(n, NULL) + assert_not_reached(); + + assert_se(bitmap_set(b, 0) == 0); + assert_se(bitmap_set(b, 1) == 0); + assert_se(bitmap_set(b, 256) == 0); + + BITMAP_FOREACH(n, b) { + assert_se(n == i); + if (i == 0) + i = 1; + else if (i == 1) + i = 256; + else if (i == 256) + i = UINT_MAX; + } + + assert_se(i == UINT_MAX); + + i = 0; + + BITMAP_FOREACH(n, b) { + assert_se(n == i); + if (i == 0) + i = 1; + else if (i == 1) + i = 256; + else if (i == 256) + i = UINT_MAX; + } + + assert_se(i == UINT_MAX); + + b2 = bitmap_copy(b); + assert_se(b2); + assert_se(bitmap_equal(b, b2) == true); + assert_se(bitmap_equal(b, b) == true); + assert_se(bitmap_equal(b, NULL) == false); + assert_se(bitmap_equal(NULL, b) == false); + assert_se(bitmap_equal(NULL, NULL) == true); + + bitmap_clear(b); + assert_se(bitmap_isclear(b) == true); + assert_se(bitmap_equal(b, b2) == false); + b2 = bitmap_free(b2); + + assert_se(bitmap_set(b, UINT_MAX) == -ERANGE); + + b = bitmap_free(b); + assert_se(bitmap_ensure_allocated(&b) == 0); + assert_se(bitmap_ensure_allocated(&b2) == 0); + + assert_se(bitmap_equal(b, b2)); + assert_se(bitmap_set(b, 0) == 0); + bitmap_unset(b, 0); + assert_se(bitmap_equal(b, b2)); + + assert_se(bitmap_set(b, 1) == 0); + bitmap_clear(b); + assert_se(bitmap_equal(b, b2)); + + assert_se(bitmap_set(b, 0) == 0); + assert_se(bitmap_set(b2, 0) == 0); + assert_se(bitmap_equal(b, b2)); + + return 0; +} |