Skip to content

Commit

Permalink
fixup_named_flags(): Add a few unit tests
Browse files Browse the repository at this point in the history
It's easy to lose track of how this function works, given there are 3
different ways of invoking it.  So write a few tests to "lock in" its
behavior and also make it available for double-checking on demand.
  • Loading branch information
liviuchircu committed Oct 11, 2023
1 parent b3990ad commit 6eedadb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
77 changes: 77 additions & 0 deletions test/test_ut.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <tap.h>

#include "../ut.h"
#include "../str.h"
#include "../mod_fix.h"

void test_ut(void)
{
Expand All @@ -45,4 +47,79 @@ void test_ut(void)
ok(_is_e164(_str("+123456789012345"), 0, 15) == 1, "test-e164-17");
ok(_is_e164(_str("+1234567890123456"), 0, 15) == -1, "test-e164-18");
ok(_is_e164(_str("123456789x12345"), 0, 15) == -1, "test-e164-19");

/* named flags -> output bitmask */
{
str f_names[] = {
str_init("A"),
str_init("B"),
str_init("C"),
str_init("D"),
str_init("E"),
STR_NULL
};
void *out = &str_init("B,D,E");

ok(!fixup_named_flags(&out, f_names, NULL, NULL), "test-fixup-flags-0");
ok((unsigned int)(unsigned long)out == (2|8|16), "test-fixup-flags-1");
}

/* named K/V flags -> output a list of values */
{
str kvf_names[] = {
str_init("A"),
str_init("B"),
str_init("C"),
str_init("D"),
str_init("E"),
STR_NULL
};
str kvf_values[sizeof(kvf_names)/sizeof(kvf_names[0])] = {};

void *out = &str_init("A=X,C=YYY,E=ZZZZZZZZZ");
ok(!fixup_named_flags(&out, NULL, kvf_names, kvf_values), "test-fixup-flags-2");

/* Note: the @out no longer gets changed now */

ok(str_match(&kvf_values[0], &str_init("X")), "test-fixup-flags-3.1");
ok(str_match(&kvf_values[1], &STR_NULL), "test-fixup-flags-3.2");
ok(str_match(&kvf_values[2], &str_init("YYY")), "test-fixup-flags-3.3");
ok(str_match(&kvf_values[3], &STR_NULL), "test-fixup-flags-3.4");
ok(str_match(&kvf_values[4], &str_init("ZZZZZZZZZ")), "test-fixup-flags-3.5");
ok(str_match(&kvf_values[5], &STR_NULL), "test-fixup-flags-3.6");
}

/* combined flags test:
* - named flags -> output bitmask
* - named K/V flags -> output a list of values */
{
str f_names[] = {
str_init("A"),
str_init("B"),
str_init("C"),
str_init("D"),
str_init("E"),
STR_NULL
};
str kvf_names[] = {
str_init("KA"),
str_init("KB"),
str_init("KC"),
str_init("KD"),
str_init("KE"),
STR_NULL
};
str kvf_values[sizeof(kvf_names)/sizeof(kvf_names[0])] = {};

void *out = &str_init("E,KB=X,D,A,KC=YYY,B,KE=ZZZZZZZZZ");
ok(!fixup_named_flags(&out, f_names, kvf_names, kvf_values), "test-fixup-flags-4");
ok((unsigned int)(unsigned long)out == (1|2|8|16), "test-fixup-flags-5");

ok(str_match(&kvf_values[0], &STR_NULL), "test-fixup-flags-3.1");
ok(str_match(&kvf_values[1], &str_init("X")), "test-fixup-flags-3.2");
ok(str_match(&kvf_values[2], &str_init("YYY")), "test-fixup-flags-3.3");
ok(str_match(&kvf_values[3], &STR_NULL), "test-fixup-flags-3.4");
ok(str_match(&kvf_values[4], &str_init("ZZZZZZZZZ")), "test-fixup-flags-3.5");
ok(str_match(&kvf_values[5], &STR_NULL), "test-fixup-flags-3.6");
}
}
2 changes: 2 additions & 0 deletions test/test_ut.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#ifndef TEST_UT_H
#define TEST_UT_H

/* Test various utility functions, mainly from OpenSIPS core */

void test_ut(void);

#endif

0 comments on commit 6eedadb

Please sign in to comment.