diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/printf.c | 16 | ||||
-rw-r--r-- | lib/printf_test.c | 37 | ||||
-rw-r--r-- | lib/string.h | 9 |
3 files changed, 54 insertions, 8 deletions
diff --git a/lib/printf.c b/lib/printf.c index c2065d9a..8f2cccb3 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -356,14 +356,14 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args) if (qualifier == 'l') { X = va_arg(args, u64); bsprintf(ipbuf, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", - ((X >> 56) & 0xff), - ((X >> 48) & 0xff), - ((X >> 40) & 0xff), - ((X >> 32) & 0xff), - ((X >> 24) & 0xff), - ((X >> 16) & 0xff), - ((X >> 8) & 0xff), - (X & 0xff)); + (uint) ((X >> 56) & 0xff), + (uint) ((X >> 48) & 0xff), + (uint) ((X >> 40) & 0xff), + (uint) ((X >> 32) & 0xff), + (uint) ((X >> 24) & 0xff), + (uint) ((X >> 16) & 0xff), + (uint) ((X >> 8) & 0xff), + (uint) (X & 0xff)); } else { diff --git a/lib/printf_test.c b/lib/printf_test.c index a2683d93..341fde9c 100644 --- a/lib/printf_test.c +++ b/lib/printf_test.c @@ -56,6 +56,27 @@ t_simple(void) BSPRINTF(2, "-1", buf, "%d", -1); BSPRINTF(11, "-2147483648", buf, "%d", -2147483648); + return 1; +} + +static int +t_router_id(void) +{ + char buf[256]; + + BSPRINTF(7, "1.2.3.4", buf, "%R", (u32) 0x01020304); + BSPRINTF(15, "240.224.208.192", buf, "%R", (u32) 0xF0E0D0C0); + BSPRINTF(23, "01:02:03:04:05:06:07:08", buf, "%lR", (u64) 0x0102030405060708); + BSPRINTF(23, "f0:e0:d0:c0:b0:a0:90:80", buf, "%lR", (u64) 0xF0E0D0C0B0A09080); + + return 1; +} + +static int +t_time(void) +{ + char buf[256]; + BSPRINTF(7, "123.456", buf, "%t", (btime) 123456789); BSPRINTF(7, "123.456", buf, "%2t", (btime) 123456789); BSPRINTF(8, " 123.456", buf, "%8t", (btime) 123456789); @@ -68,12 +89,28 @@ t_simple(void) return 1; } +static int +t_bstrcmp(void) +{ + bt_assert(bstrcmp("aa", "aa") == 0); + bt_assert(bstrcmp("aa", "bb") == -1); + bt_assert(bstrcmp("bb", "aa") == 1); + bt_assert(bstrcmp(NULL, NULL) == 0); + bt_assert(bstrcmp(NULL, "bb") == -1); + bt_assert(bstrcmp("bb", NULL) == 1); + + return 1; +} + int main(int argc, char *argv[]) { bt_init(argc, argv); bt_test_suite(t_simple, "printf without varargs"); + bt_test_suite(t_router_id, "print router id"); + bt_test_suite(t_time, "print time"); + bt_test_suite(t_bstrcmp, "bstrcmp"); return bt_exit_value(); } diff --git a/lib/string.h b/lib/string.h index 6e549cb7..d6ae5ef7 100644 --- a/lib/string.h +++ b/lib/string.h @@ -63,6 +63,15 @@ memset32(void *D, u32 val, uint n) dst[i] = val; } +static inline int +bstrcmp(const char *s1, const char *s2) +{ + if (s1 && s2) + return strcmp(s1, s2); + else + return !s2 - !s1; +} + #define ROUTER_ID_64_LENGTH 23 #endif |