From aae230a32125e93fff9c014aab57d619d38a8cea Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Thu, 27 Dec 2007 00:19:09 +0100 Subject: [PATCH] Add util functions to deal with ISO types. --- src/util.c | 22 ++++++++++ src/util.h | 3 ++ test/test_util.c | 112 ++++++++++++++++++++++++++++++++++------------- 3 files changed, 107 insertions(+), 30 deletions(-) diff --git a/src/util.c b/src/util.c index e850388..65248cd 100644 --- a/src/util.c +++ b/src/util.c @@ -514,6 +514,28 @@ void iso_bb(uint8_t *buf, uint32_t num, int bytes) iso_msb(buf+bytes, num, bytes); } +uint32_t iso_read_lsb(const uint8_t *buf, int bytes) +{ + int i; + uint32_t ret = 0; + + for (i=0; i static void test_div_up() { - CU_ASSERT_EQUAL( div_up(1, 2), 1 ); - CU_ASSERT_EQUAL( div_up(2, 2), 1 ); - CU_ASSERT_EQUAL( div_up(0, 2), 0 ); - CU_ASSERT_EQUAL( div_up(-1, 2), 0 ); - CU_ASSERT_EQUAL( div_up(3, 2), 2 ); + CU_ASSERT_EQUAL( div_up(1, 2), 1 ); + CU_ASSERT_EQUAL( div_up(2, 2), 1 ); + CU_ASSERT_EQUAL( div_up(0, 2), 0 ); + CU_ASSERT_EQUAL( div_up(-1, 2), 0 ); + CU_ASSERT_EQUAL( div_up(3, 2), 2 ); } static void test_round_up() { - CU_ASSERT_EQUAL( round_up(1, 2), 2 ); - CU_ASSERT_EQUAL( round_up(2, 2), 2 ); - CU_ASSERT_EQUAL( round_up(0, 2), 0 ); - CU_ASSERT_EQUAL( round_up(-1, 2), 0 ); - CU_ASSERT_EQUAL( round_up(3, 2), 4 ); - CU_ASSERT_EQUAL( round_up(15, 7), 21 ); - CU_ASSERT_EQUAL( round_up(13, 7), 14 ); - CU_ASSERT_EQUAL( round_up(14, 7), 14 ); + CU_ASSERT_EQUAL( round_up(1, 2), 2 ); + CU_ASSERT_EQUAL( round_up(2, 2), 2 ); + CU_ASSERT_EQUAL( round_up(0, 2), 0 ); + CU_ASSERT_EQUAL( round_up(-1, 2), 0 ); + CU_ASSERT_EQUAL( round_up(3, 2), 4 ); + CU_ASSERT_EQUAL( round_up(15, 7), 21 ); + CU_ASSERT_EQUAL( round_up(13, 7), 14 ); + CU_ASSERT_EQUAL( round_up(14, 7), 14 ); +} + +static void test_iso_lsb_msb() +{ + uint8_t buf[4]; + uint32_t num; + + num = 0x01020304; + iso_lsb(buf, num, 4); + CU_ASSERT_EQUAL( buf[0], 0x04 ); + CU_ASSERT_EQUAL( buf[1], 0x03 ); + CU_ASSERT_EQUAL( buf[2], 0x02 ); + CU_ASSERT_EQUAL( buf[3], 0x01 ); + + iso_msb(buf, num, 4); + CU_ASSERT_EQUAL( buf[0], 0x01 ); + CU_ASSERT_EQUAL( buf[1], 0x02 ); + CU_ASSERT_EQUAL( buf[2], 0x03 ); + CU_ASSERT_EQUAL( buf[3], 0x04 ); + + iso_lsb(buf, num, 2); + CU_ASSERT_EQUAL( buf[0], 0x04 ); + CU_ASSERT_EQUAL( buf[1], 0x03 ); + + iso_msb(buf, num, 2); + CU_ASSERT_EQUAL( buf[0], 0x03 ); + CU_ASSERT_EQUAL( buf[1], 0x04 ); +} + +static void test_iso_read_lsb_msb() +{ + uint8_t buf[4]; + uint32_t num; + + buf[0] = 0x04; + buf[1] = 0x03; + buf[2] = 0x02; + buf[3] = 0x01; + + num = iso_read_lsb(buf, 4); + CU_ASSERT_EQUAL(num, 0x01020304); + + num = iso_read_msb(buf, 4); + CU_ASSERT_EQUAL(num, 0x04030201); + + num = iso_read_lsb(buf, 2); + CU_ASSERT_EQUAL(num, 0x0304); + + num = iso_read_msb(buf, 2); + CU_ASSERT_EQUAL(num, 0x0403); } static void test_iso_bb() { uint8_t buf[8]; uint32_t num; - + num = 0x01020304; iso_bb(buf, num, 4); CU_ASSERT_EQUAL( buf[0], 0x04 ); @@ -44,7 +94,7 @@ static void test_iso_bb() CU_ASSERT_EQUAL( buf[5], 0x02 ); CU_ASSERT_EQUAL( buf[6], 0x03 ); CU_ASSERT_EQUAL( buf[7], 0x04 ); - + iso_bb(buf, num, 2); CU_ASSERT_EQUAL( buf[0], 0x04 ); CU_ASSERT_EQUAL( buf[1], 0x03 ); @@ -134,58 +184,60 @@ static void test_iso_rbtree_insert() IsoRBTree *tree; char *str1, *str2, *str3, *str4, *str5; void *str; - + res = iso_rbtree_new(strcmp, &tree); CU_ASSERT_EQUAL(res, 1); - + /* ok, insert one str */ str1 = "first str"; res = iso_rbtree_insert(tree, str1, &str); CU_ASSERT_EQUAL(res, 1); CU_ASSERT_PTR_EQUAL(str, str1); - + str2 = "second str"; res = iso_rbtree_insert(tree, str2, &str); CU_ASSERT_EQUAL(res, 1); CU_ASSERT_PTR_EQUAL(str, str2); - + /* an already inserted string */ str3 = "second str"; res = iso_rbtree_insert(tree, str3, &str); CU_ASSERT_EQUAL(res, 0); CU_ASSERT_PTR_EQUAL(str, str2); - + /* an already inserted string */ str3 = "first str"; res = iso_rbtree_insert(tree, str3, &str); CU_ASSERT_EQUAL(res, 0); CU_ASSERT_PTR_EQUAL(str, str1); - + str4 = "a string to be inserted first"; res = iso_rbtree_insert(tree, str4, &str); CU_ASSERT_EQUAL(res, 1); CU_ASSERT_PTR_EQUAL(str, str4); - + str5 = "this to be inserted last"; res = iso_rbtree_insert(tree, str5, &str); CU_ASSERT_EQUAL(res, 1); CU_ASSERT_PTR_EQUAL(str, str5); - + /* * TODO write a really good test to check all possible estrange * behaviors of a red-black tree */ - + iso_rbtree_destroy(tree, NULL); } -void add_util_suite() +void add_util_suite() { - CU_pSuite pSuite = CU_add_suite("UtilSuite", NULL, NULL); - - CU_add_test(pSuite, "div_up()", test_div_up); - CU_add_test(pSuite, "round_up()", test_round_up); + CU_pSuite pSuite = CU_add_suite("UtilSuite", NULL, NULL); + + CU_add_test(pSuite, "div_up()", test_div_up); + CU_add_test(pSuite, "round_up()", test_round_up); CU_add_test(pSuite, "iso_bb()", test_iso_bb); + CU_add_test(pSuite, "iso_lsb/msb()", test_iso_lsb_msb); + CU_add_test(pSuite, "iso_read_lsb/msb()", test_iso_read_lsb_msb); CU_add_test(pSuite, "iso_1_dirid()", test_iso_1_dirid); CU_add_test(pSuite, "iso_2_dirid()", test_iso_2_dirid); CU_add_test(pSuite, "iso_1_fileid()", test_iso_1_fileid);