"Fossies" - the Fresh Open Source Software Archive

Member "mlr-5.6.2/c/unit_test/test_mlrutil.c" (25 Aug 2019, 8713 Bytes) of package /linux/misc/mlr-5.6.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include <stdio.h>
    2 #include <string.h>
    3 #include "lib/minunit.h"
    4 #include "lib/mlr_globals.h"
    5 #include "lib/mlrutil.h"
    6 
    7 int tests_run         = 0;
    8 int tests_failed      = 0;
    9 int assertions_run    = 0;
   10 int assertions_failed = 0;
   11 
   12 // ----------------------------------------------------------------
   13 static char * test_canonical_mod() {
   14     mu_assert("error: canonical_mod -7", mlr_canonical_mod(-7, 5) == 3);
   15     mu_assert("error: canonical_mod -6", mlr_canonical_mod(-6, 5) == 4);
   16     mu_assert("error: canonical_mod -5", mlr_canonical_mod(-5, 5) == 0);
   17     mu_assert("error: canonical_mod -4", mlr_canonical_mod(-4, 5) == 1);
   18     mu_assert("error: canonical_mod -3", mlr_canonical_mod(-3, 5) == 2);
   19     mu_assert("error: canonical_mod -2", mlr_canonical_mod(-2, 5) == 3);
   20     mu_assert("error: canonical_mod -1", mlr_canonical_mod(-1, 5) == 4);
   21     mu_assert("error: canonical_mod  0", mlr_canonical_mod(0, 5) == 0);
   22     mu_assert("error: canonical_mod  1", mlr_canonical_mod(1, 5) == 1);
   23     mu_assert("error: canonical_mod  2", mlr_canonical_mod(2, 5) == 2);
   24     mu_assert("error: canonical_mod  3", mlr_canonical_mod(3, 5) == 3);
   25     mu_assert("error: canonical_mod  4", mlr_canonical_mod(4, 5) == 4);
   26     mu_assert("error: canonical_mod  5", mlr_canonical_mod(5, 5) == 0);
   27     mu_assert("error: canonical_mod  6", mlr_canonical_mod(6, 5) == 1);
   28     mu_assert("error: canonical_mod  7", mlr_canonical_mod(7, 5) == 2);
   29     return 0;
   30 }
   31 
   32 // ----------------------------------------------------------------
   33 static char * test_power_of_two_above() {
   34     mu_assert("error: power_of_two_above 0", power_of_two_above(0) == 1);
   35     mu_assert("error: power_of_two_above 1", power_of_two_above(1) == 2);
   36     mu_assert("error: power_of_two_above 2", power_of_two_above(2) == 4);
   37     mu_assert("error: power_of_two_above 3", power_of_two_above(3) == 4);
   38     mu_assert("error: power_of_two_above 4", power_of_two_above(4) == 8);
   39     mu_assert("error: power_of_two_above 5", power_of_two_above(5) == 8);
   40     mu_assert("error: power_of_two_above 6", power_of_two_above(6) == 8);
   41     mu_assert("error: power_of_two_above 7", power_of_two_above(7) == 8);
   42     mu_assert("error: power_of_two_above 8", power_of_two_above(8) == 16);
   43     mu_assert("error: power_of_two_above 9", power_of_two_above(9) == 16);
   44     mu_assert("error: power_of_two_above 1023", power_of_two_above(1023) == 1024);
   45     mu_assert("error: power_of_two_above 1024", power_of_two_above(1024) == 2048);
   46     mu_assert("error: power_of_two_above 1025", power_of_two_above(1025) == 2048);
   47     return 0;
   48 }
   49 
   50 // ----------------------------------------------------------------
   51 static char * test_streq() {
   52     char* x;
   53     char* y;
   54 
   55     x = "";   y = "";   mu_assert_lf(streq(x, y) == !strcmp(x, y));
   56     x = "";   y = "1";  mu_assert_lf(streq(x, y) == !strcmp(x, y));
   57     x = "";   y = "12"; mu_assert_lf(streq(x, y) == !strcmp(x, y));
   58 
   59     x = "";   y = "";   mu_assert_lf(streq(x, y) == !strcmp(x, y));
   60     x = "a";  y = "";   mu_assert_lf(streq(x, y) == !strcmp(x, y));
   61     x = "ab"; y = "";   mu_assert_lf(streq(x, y) == !strcmp(x, y));
   62 
   63     x = "1";  y = "";   mu_assert_lf(streq(x, y) == !strcmp(x, y));
   64     x = "1";  y = "1";  mu_assert_lf(streq(x, y) == !strcmp(x, y));
   65     x = "1";  y = "12"; mu_assert_lf(streq(x, y) == !strcmp(x, y));
   66 
   67     x = "12"; y = "";   mu_assert_lf(streq(x, y) == !strcmp(x, y));
   68     x = "12"; y = "1";  mu_assert_lf(streq(x, y) == !strcmp(x, y));
   69     x = "12"; y = "12"; mu_assert_lf(streq(x, y) == !strcmp(x, y));
   70 
   71     x = "";   y = "a";  mu_assert_lf(streq(x, y) == !strcmp(x, y));
   72     x = "a";  y = "a";  mu_assert_lf(streq(x, y) == !strcmp(x, y));
   73     x = "ab"; y = "a";  mu_assert_lf(streq(x, y) == !strcmp(x, y));
   74 
   75     x = "";   y = "ab"; mu_assert_lf(streq(x, y) == !strcmp(x, y));
   76     x = "a";  y = "ab"; mu_assert_lf(streq(x, y) == !strcmp(x, y));
   77     x = "ab"; y = "ab"; mu_assert_lf(streq(x, y) == !strcmp(x, y));
   78 
   79     return 0;
   80 }
   81 
   82 // ----------------------------------------------------------------
   83 static char * test_streqn() {
   84     char* x;
   85     char* y;
   86 
   87     x = "";    y = "";    mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
   88     x = "";    y = "1";   mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
   89     x = "";    y = "12";  mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
   90     x = "";    y = "123"; mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
   91 
   92     x = "";    y = "";    mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
   93     x = "a";   y = "";    mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
   94     x = "ab";  y = "";    mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
   95     x = "abc"; y = "";    mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
   96 
   97     x = "a";   y = "a";   mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
   98     x = "a";   y = "aa";  mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
   99     x = "a";   y = "ab";  mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
  100     x = "a";   y = "abd"; mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
  101 
  102     x = "ab";  y = "a";   mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
  103     x = "ab";  y = "ab";  mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
  104     x = "ab";  y = "abd"; mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
  105 
  106     x = "abc"; y = "a";   mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
  107     x = "abc"; y = "ab";  mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
  108     x = "abc"; y = "abc"; mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
  109     x = "abc"; y = "abd"; mu_assert_lf(streqn(x, y, 2) == !strncmp(x, y, 2));
  110 
  111     return 0;
  112 }
  113 
  114 // ----------------------------------------------------------------
  115 static char * test_strdup_quoted() {
  116 
  117     mu_assert_lf(streq(mlr_strdup_quoted_or_die(""), "\"\""));
  118     mu_assert_lf(streq(mlr_strdup_quoted_or_die("x"), "\"x\""));
  119     mu_assert_lf(streq(mlr_strdup_quoted_or_die("xy"), "\"xy\""));
  120     mu_assert_lf(streq(mlr_strdup_quoted_or_die("xyz"), "\"xyz\""));
  121 
  122     return 0;
  123 }
  124 
  125 // ----------------------------------------------------------------
  126 static char * test_starts_or_ends_with() {
  127 
  128     mu_assert_lf(string_starts_with("abcde", ""));
  129     mu_assert_lf(string_starts_with("abcde", "a"));
  130     mu_assert_lf(string_starts_with("abcde", "abcd"));
  131     mu_assert_lf(string_starts_with("abcde", "abcde"));
  132     mu_assert_lf(!string_starts_with("abcde", "abcdef"));
  133 
  134     mu_assert_lf(string_ends_with("abcde", "", NULL));
  135     mu_assert_lf(string_ends_with("abcde", "e", NULL));
  136     mu_assert_lf(string_ends_with("abcde", "de", NULL));
  137     mu_assert_lf(string_ends_with("abcde", "abcde", NULL));
  138     mu_assert_lf(!string_ends_with("abcde", "0abcde", NULL));
  139     int len = -1;
  140     mu_assert_lf(!string_ends_with("abcde", "0abcde", &len));
  141     mu_assert_lf(len == 5);
  142 
  143     return 0;
  144 }
  145 
  146 // ----------------------------------------------------------------
  147 static char * test_scanners() {
  148     mu_assert("error: mlr_alloc_string_from_double", streq(mlr_alloc_string_from_double(4.25, "%.4f"), "4.2500"));
  149     mu_assert("error: mlr_alloc_string_from_ull", streq(mlr_alloc_string_from_ull(12345LL), "12345"));
  150     mu_assert("error: mlr_alloc_string_from_int", streq(mlr_alloc_string_from_int(12345), "12345"));
  151     return 0;
  152 }
  153 
  154 // ----------------------------------------------------------------
  155 static char * test_paste() {
  156     mu_assert("error: paste 2", streq(mlr_paste_2_strings("ab", "cd"), "abcd"));
  157     mu_assert("error: paste 3", streq(mlr_paste_3_strings("ab", "cd", "ef"), "abcdef"));
  158     mu_assert("error: paste 4", streq(mlr_paste_4_strings("ab", "cd", "ef", "gh"), "abcdefgh"));
  159     mu_assert("error: paste 5", streq(mlr_paste_5_strings("ab", "cd", "ef", "gh", "ij"), "abcdefghij"));
  160     return 0;
  161 }
  162 
  163 // ----------------------------------------------------------------
  164 static char * test_unbackslash() {
  165     mu_assert_lf(streq(mlr_alloc_unbackslash(""), ""));
  166     mu_assert_lf(streq(mlr_alloc_unbackslash("hello"), "hello"));
  167     mu_assert_lf(streq(mlr_alloc_unbackslash("\\r\\n"), "\r\n"));
  168     mu_assert_lf(streq(mlr_alloc_unbackslash("\\t\\\\"), "\t\\"));
  169     mu_assert_lf(streq(mlr_alloc_unbackslash("[\\132]"), "[Z]"));
  170     mu_assert_lf(streq(mlr_alloc_unbackslash("[\\x59]"), "[Y]"));
  171     return 0;
  172 }
  173 
  174 // ================================================================
  175 static char * all_tests() {
  176     mu_run_test(test_canonical_mod);
  177     mu_run_test(test_power_of_two_above);
  178     mu_run_test(test_streq);
  179     mu_run_test(test_streqn);
  180     mu_run_test(test_strdup_quoted);
  181     mu_run_test(test_starts_or_ends_with);
  182     mu_run_test(test_scanners);
  183     mu_run_test(test_paste);
  184     mu_run_test(test_unbackslash);
  185     return 0;
  186 }
  187 
  188 int main(int argc, char **argv) {
  189     mlr_global_init(argv[0], NULL);
  190     printf("TEST_MLRUTIL ENTER\n");
  191     char *result = all_tests();
  192     printf("\n");
  193     if (result != 0) {
  194         printf("Not all unit tests passed\n");
  195     }
  196     else {
  197         printf("TEST_MLRUTIL: ALL UNIT TESTS PASSED\n");
  198     }
  199     printf("Tests      passed: %d of %d\n", tests_run - tests_failed, tests_run);
  200     printf("Assertions passed: %d of %d\n", assertions_run - assertions_failed, assertions_run);
  201 
  202     return result != 0;
  203 }