int main(int argc, char *argv[]) { char buf[CONST_MAX(FORMAT_TIMESPAN_MAX, FORMAT_BYTES_MAX)]; nsec_t nsec; uint64_t v; int r; log_parse_environment(); log_open(); assert_se(procfs_cpu_get_usage(&nsec) >= 0); log_info("Current system CPU time: %s", format_timespan(buf, sizeof(buf), nsec/NSEC_PER_USEC, 1)); assert_se(procfs_memory_get_current(&v) >= 0); log_info("Current memory usage: %s", format_bytes(buf, sizeof(buf), v)); assert_se(procfs_tasks_get_current(&v) >= 0); log_info("Current number of tasks: %" PRIu64, v); assert_se(procfs_tasks_get_limit(&v) >= 0); log_info("Limit of tasks: %" PRIu64, v); assert_se(v > 0); assert_se(procfs_tasks_set_limit(v) >= 0); if (v > 100) { uint64_t w; r = procfs_tasks_set_limit(v-1); assert_se(IN_SET(r, 0, -EPERM, -EACCES, -EROFS)); assert_se(procfs_tasks_get_limit(&w) >= 0); assert_se((r == 0 && w == v - 1) || (r < 0 && w == v)); assert_se(procfs_tasks_set_limit(v) >= 0); assert_se(procfs_tasks_get_limit(&w) >= 0); assert_se(v == w); } return 0; }
static void test_max(void) { static const struct { int a; int b[CONST_MAX(10, 100)]; } val1 = { .a = CONST_MAX(10, 100), }; int d = 0; unsigned long x = 12345; unsigned long y = 54321; const char str[] = "a_string_constant"; const unsigned long long arr[] = {9999ULL, 10ULL, 0ULL, 3000ULL, 2000ULL, 1000ULL, 100ULL, 9999999ULL}; void *p = (void *)str; void *q = (void *)&str[16]; log_info("/* %s */", __func__); assert_cc(sizeof(val1.b) == sizeof(int) * 100); /* CONST_MAX returns (void) instead of a value if the passed arguments * are not of the same type or not constant expressions. */ assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int)); assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void)); assert_se(val1.a == 100); assert_se(MAX(++d, 0) == 1); assert_se(d == 1); 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(CMP(3, 5) == -1); assert_se(CMP(5, 3) == 1); assert_se(CMP(5, 5) == 0); assert_se(CMP(x, y) == -1); assert_se(CMP(y, x) == 1); assert_se(CMP(x, x) == 0); assert_se(CMP(y, y) == 0); assert_se(CMP(UINT64_MAX, (uint64_t) 0) == 1); assert_se(CMP((uint64_t) 0, UINT64_MAX) == -1); assert_se(CMP(UINT64_MAX, UINT64_MAX) == 0); assert_se(CMP(INT64_MIN, INT64_MAX) == -1); assert_se(CMP(INT64_MAX, INT64_MIN) == 1); assert_se(CMP(INT64_MAX, INT64_MAX) == 0); assert_se(CMP(INT64_MIN, INT64_MIN) == 0); assert_se(CMP(INT64_MAX, (int64_t) 0) == 1); assert_se(CMP((int64_t) 0, INT64_MIN) == 1); assert_se(CMP(INT64_MIN, (int64_t) 0) == -1); assert_se(CMP((int64_t) 0, INT64_MAX) == -1); assert_se(CMP(&str[2], &str[7]) == -1); assert_se(CMP(&str[2], &str[2]) == 0); assert_se(CMP(&str[7], (const char *)str) == 1); assert_se(CMP(str[2], str[7]) == 1); assert_se(CMP(str[7], *str) == 1); assert_se(CMP((const unsigned long long *)arr, &arr[3]) == -1); assert_se(CMP(*arr, arr[3]) == 1); assert_se(CMP(p, q) == -1); assert_se(CMP(q, p) == 1); assert_se(CMP(p, p) == 0); assert_se(CMP(q, q) == 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); } #pragma GCC diagnostic push #ifdef __clang__ # pragma GCC diagnostic ignored "-Waddress-of-packed-member" #endif static void test_container_of(void) { struct mytype { uint8_t pad1[3]; uint64_t v1; uint8_t pad2[2]; uint32_t v2; } myval = { }; log_info("/* %s */", __func__); 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); }
[IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, [IFLA_CARRIER] = { .type = NLA_U8 }, /* [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN }, */ }; static const NLTypeSystem rtnl_link_type_system = { .max = ELEMENTSOF(rtnl_link_types) - 1, .types = rtnl_link_types, }; /* IFA_FLAGS was defined in kernel 3.14, but we still support older * kernels where IFA_MAX is lower. */ static const NLType rtnl_address_types[CONST_MAX(IFA_MAX, IFA_FLAGS) + 1] = { [IFA_ADDRESS] = { .type = NLA_IN_ADDR }, [IFA_LOCAL] = { .type = NLA_IN_ADDR }, [IFA_LABEL] = { .type = NLA_STRING, .size = IFNAMSIZ - 1 }, [IFA_BROADCAST] = { .type = NLA_IN_ADDR }, /* 6? */ [IFA_CACHEINFO] = { .type = NLA_CACHE_INFO, .size = sizeof(struct ifa_cacheinfo) }, /* [IFA_ANYCAST], [IFA_MULTICAST], */ [IFA_FLAGS] = { .type = NLA_U32 }, }; static const NLTypeSystem rtnl_address_type_system = { .max = ELEMENTSOF(rtnl_address_types) - 1, .types = rtnl_address_types,
static void test_max(void) { static const struct { int a; int b[CONST_MAX(10, 100)]; } val1 = { .a = CONST_MAX(10, 100), }; int d = 0; assert_cc(sizeof(val1.b) == sizeof(int) * 100); /* CONST_MAX returns (void) instead of a value if the passed arguments * are not of the same type or not constant expressions. */ assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int)); assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void)); assert_se(val1.a == 100); assert_se(MAX(++d, 0) == 1); assert_se(d == 1); 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); } #pragma GCC diagnostic push #ifdef __clang__ # pragma GCC diagnostic ignored "-Waddress-of-packed-member" #endif 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); }