00001
00002 #include <check.h>
00003
00004 #include <bits.h>
00005
00006 START_TEST(test_getbitu)
00007 {
00008 u8 test_data[] = {
00009 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
00010 };
00011
00012 u32 ret;
00013
00014 ret = getbitu(test_data, 0, 8);
00015 fail_unless(ret == 0x01,
00016 "Test case 1 expected 0x01, got 0x%02X", ret);
00017
00018 ret = getbitu(test_data, 4, 8);
00019 fail_unless(ret == 0x12,
00020 "test case 2 expected 0x12, got 0x%02X", ret);
00021
00022 ret = getbitu(test_data, 28, 16);
00023 fail_unless(ret == 0x789A,
00024 "test case 3 expected 0x789A, got 0x%04X", ret);
00025
00026 ret = getbitu(test_data, 12, 32);
00027 fail_unless(ret == 0x3456789A,
00028 "test case 4 expected 0x3456789A, got 0x%08X", ret);
00029
00030 ret = getbitu(test_data, 10, 3);
00031 fail_unless(ret == 0x4,
00032 "test case 5 expected 0x4, got 0x%01X", ret);
00033
00034 ret = getbitu(test_data, 10, 13);
00035 fail_unless(ret == 0x11A2,
00036 "test case 6 expected 0x11A2, got 0x%04X", ret);
00037 }
00038 END_TEST
00039
00040 START_TEST(test_getbits)
00041 {
00042 u8 test_data[] = {
00043 0x00, 0x03, 0x80, 0xFF, 0xFF, 0xFF, 0xFF
00044 };
00045
00046 s32 ret;
00047
00048 ret = getbits(test_data, 0, 8);
00049 fail_unless(ret == 0,
00050 "Test case 1 expected 0, got %d", ret);
00051
00052 ret = getbits(test_data, 13, 3);
00053 fail_unless(ret == 3,
00054 "Test case 2 expected 3, got %d", ret);
00055
00056 ret = getbits(test_data, 14, 3);
00057 fail_unless(ret == -1,
00058 "Test case 3 expected -1, got %d", ret);
00059
00060 ret = getbits(test_data, 14, 4);
00061 fail_unless(ret == -2,
00062 "Test case 4 expected -2, got %d", ret);
00063
00064 ret = getbits(test_data, 24, 32);
00065 fail_unless(ret == -1,
00066 "Test case 5 expected -1, got %d", ret);
00067 }
00068 END_TEST
00069
00070 START_TEST(test_setbitu)
00071 {
00072 u8 test_data[10];
00073
00074 u32 ret;
00075
00076 setbitu(test_data, 10, 13, 0x11A2);
00077 ret = getbitu(test_data, 10, 13);
00078 fail_unless(ret == 0x11A2,
00079 "test case 1 expected 0x11A2, got 0x%04X", ret);
00080
00081
00082
00083 }
00084 END_TEST
00085
00086 START_TEST(test_setbits)
00087 {
00088 u8 test_data[10];
00089
00090 s32 ret;
00091
00092 setbits(test_data, 14, 3, -1);
00093 ret = getbits(test_data, 14, 3);
00094 fail_unless(ret == -1,
00095 "Test case 1 expected -1, got %d", ret);
00096
00097 setbits(test_data, 14, 8, 22);
00098 ret = getbits(test_data, 14, 8);
00099 fail_unless(ret == 22,
00100 "Test case 2 expected 22, got %d", ret);
00101
00102 setbits(test_data, 24, 32, -1);
00103 ret = getbits(test_data, 24, 32);
00104 fail_unless(ret == -1,
00105 "Test case 3 expected -1, got %d", ret);
00106 }
00107 END_TEST
00108
00109 Suite* bits_suite(void)
00110 {
00111 Suite *s = suite_create("Bit Utils");
00112
00113 TCase *tc_core = tcase_create("Core");
00114 tcase_add_test(tc_core, test_getbitu);
00115 tcase_add_test(tc_core, test_getbits);
00116 tcase_add_test(tc_core, test_setbitu);
00117 tcase_add_test(tc_core, test_setbits);
00118 suite_add_tcase(s, tc_core);
00119
00120 return s;
00121 }
00122