X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Ftest%2Ftest-util.c;h=80425ca61a3aa176ae7e7564147e756e0f02e732;hb=3f2e132a197ba3f5172cbbcd285e9aab021de8fc;hp=4d9b28f9c8e7b82aca0440a0b2c56c8d86b06789;hpb=684fc8927e0f83496d4384ac434e265f7cd7a87b;p=elogind.git diff --git a/src/test/test-util.c b/src/test/test-util.c index 4d9b28f9c..80425ca61 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -94,6 +94,41 @@ static void test_max(void) { assert_cc(MAXSIZE(char[3], uint16_t) == 3); assert_cc(MAXSIZE(char[3], uint32_t) == 4); assert_cc(MAXSIZE(char, long) == sizeof(long)); + + assert_se(MAX(-5, 5) == 5); + assert_se(MAX(5, 5) == 5); + assert_se(MAX(MAX(1, MAX(2, MAX(3, 4))), 5) == 5); + assert_se(MAX(MAX(1, MAX(2, MAX(3, 2))), 1) == 3); + assert_se(MAX(MIN(1, MIN(2, MIN(3, 4))), 5) == 5); + assert_se(MAX(MAX(1, MIN(2, MIN(3, 2))), 1) == 2); + assert_se(LESS_BY(8, 4) == 4); + assert_se(LESS_BY(8, 8) == 0); + assert_se(LESS_BY(4, 8) == 0); + assert_se(LESS_BY(16, LESS_BY(8, 4)) == 12); + assert_se(LESS_BY(4, LESS_BY(8, 4)) == 0); + assert_se(CLAMP(-5, 0, 1) == 0); + assert_se(CLAMP(5, 0, 1) == 1); + assert_se(CLAMP(5, -10, 1) == 1); + assert_se(CLAMP(5, -10, 10) == 5); + assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10); +} + +static void test_container_of(void) { + struct mytype { + uint8_t pad1[3]; + uint64_t v1; + uint8_t pad2[2]; + uint32_t v2; + } _packed_ myval = { }; + + assert_cc(sizeof(myval) == 17); + assert_se(container_of(&myval.v1, struct mytype, v1) == &myval); + assert_se(container_of(&myval.v2, struct mytype, v2) == &myval); + assert_se(container_of(&container_of(&myval.v2, + struct mytype, + v2)->v1, + struct mytype, + v1) == &myval); } static void test_first_word(void) { @@ -883,11 +918,11 @@ static void test_readlink_and_make_absolute(void) { } static void test_read_one_char(void) { + _cleanup_fclose_ FILE *file = NULL; char r; bool need_nl; char name[] = "/tmp/test-read_one_char.XXXXXX"; - _cleanup_close_ int fd = -1; - FILE *file; + int fd; fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); assert_se(fd >= 0); @@ -1218,6 +1253,7 @@ int main(int argc, char *argv[]) { test_streq_ptr(); test_align_power2(); test_max(); + test_container_of(); test_first_word(); test_close_many(); test_parse_boolean();