Home | History | Annotate | Line # | Download | only in unit
      1 #include "test/jemalloc_test.h"
      2 
      3 #include "jemalloc/internal/conf.h"
      4 
      5 TEST_BEGIN(test_conf_handle_bool_true) {
      6 	bool result = false;
      7 	bool err = conf_handle_bool("true", sizeof("true") - 1, &result);
      8 	expect_false(err, "conf_handle_bool should succeed for \"true\"");
      9 	expect_true(result, "result should be true");
     10 }
     11 TEST_END
     12 
     13 TEST_BEGIN(test_conf_handle_bool_false) {
     14 	bool result = true;
     15 	bool err = conf_handle_bool("false", sizeof("false") - 1, &result);
     16 	expect_false(err, "conf_handle_bool should succeed for \"false\"");
     17 	expect_false(result, "result should be false");
     18 }
     19 TEST_END
     20 
     21 TEST_BEGIN(test_conf_handle_bool_invalid) {
     22 	bool result = false;
     23 	bool err = conf_handle_bool("yes", sizeof("yes") - 1, &result);
     24 	expect_true(err, "conf_handle_bool should fail for \"yes\"");
     25 }
     26 TEST_END
     27 
     28 TEST_BEGIN(test_conf_handle_signed_valid) {
     29 	intmax_t result = 0;
     30 	bool     err = conf_handle_signed("5000", sizeof("5000") - 1, -1,
     31 	        INTMAX_MAX, true, false, false, &result);
     32 	expect_false(err, "Should succeed for valid value");
     33 	expect_d64_eq((int64_t)result, 5000, "result should be 5000");
     34 }
     35 TEST_END
     36 
     37 TEST_BEGIN(test_conf_handle_signed_negative) {
     38 	intmax_t result = 0;
     39 	bool err = conf_handle_signed("-1", sizeof("-1") - 1, -1, INTMAX_MAX,
     40 	    true, false, false, &result);
     41 	expect_false(err, "Should succeed for -1");
     42 	expect_d64_eq((int64_t)result, -1, "result should be -1");
     43 }
     44 TEST_END
     45 
     46 TEST_BEGIN(test_conf_handle_signed_out_of_range) {
     47 	intmax_t result = 0;
     48 	bool     err = conf_handle_signed(
     49             "5000", sizeof("5000") - 1, -1, 4999, true, true, false, &result);
     50 	expect_true(err, "Should fail for out-of-range value");
     51 }
     52 TEST_END
     53 
     54 TEST_BEGIN(test_conf_handle_char_p) {
     55 	char buf[8];
     56 	bool err;
     57 
     58 	/* Normal copy. */
     59 	err = conf_handle_char_p(
     60 	    "hello", sizeof("hello") - 1, buf, sizeof(buf));
     61 	expect_false(err, "Should succeed");
     62 	expect_str_eq(buf, "hello", "Should copy string");
     63 
     64 	/* Truncation. */
     65 	err = conf_handle_char_p(
     66 	    "longstring", sizeof("longstring") - 1, buf, sizeof(buf));
     67 	expect_false(err, "Should succeed even when truncating");
     68 	expect_str_eq(buf, "longstr", "Should truncate to dest_sz - 1");
     69 }
     70 TEST_END
     71 
     72 TEST_BEGIN(test_conf_handle_char_p_zero_dest_sz) {
     73 	char buf[4] = {'X', 'Y', 'Z', '\0'};
     74 	bool err;
     75 
     76 	err = conf_handle_char_p("abc", sizeof("abc") - 1, buf, 0);
     77 	expect_false(err, "Should succeed for zero-sized destination");
     78 	expect_c_eq(buf[0], 'X', "Zero-sized destination must not be modified");
     79 }
     80 TEST_END
     81 
     82 int
     83 main(void) {
     84 	return test(test_conf_handle_bool_true, test_conf_handle_bool_false,
     85 	    test_conf_handle_bool_invalid, test_conf_handle_signed_valid,
     86 	    test_conf_handle_signed_negative,
     87 	    test_conf_handle_signed_out_of_range, test_conf_handle_char_p,
     88 	    test_conf_handle_char_p_zero_dest_sz);
     89 }
     90