Re-organize sources
[babeltrace.git] / tests / lib / test_bitfield.c
index 5ba566f67c917b34bacd6f15af5c7c8877a51939..b9bd6d40a60bb093209ff67307b66aaca4277e4a 100644 (file)
@@ -19,7 +19,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include <babeltrace/bitfield-internal.h>
+#include "compat/bitfield.h"
 #include <time.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -161,11 +161,12 @@ void run_test_unsigned_write(unsigned int src_ui, unsigned long long src_ull)
        unsigned long long readval;
        unsigned int s, l;
 
+       /* The number of bits needed to represent 0 is 0. */
        nrbits_ui = fls_u32(src_ui);
 
        /* Write from unsigned integer src input. */
        for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
-               for (l = nrbits_ui; l < (CHAR_BIT * TEST_LEN) - s; l++) {
+               for (l = nrbits_ui; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
                        init_byte_array(target.c, TEST_LEN, 0xFF);
                        bt_bitfield_write(target.c, unsigned char, s, l, src_ui);
                        bt_bitfield_read(target.c, unsigned char, s, l, &readval);
@@ -209,11 +210,12 @@ void run_test_unsigned_write(unsigned int src_ui, unsigned long long src_ull)
        }
        pass(UNSIGNED_INT_WRITE_TEST_DESC_FMT_STR, src_ui);
 
+       /* The number of bits needed to represent 0 is 0. */
        nrbits_ull = fls_u64(src_ull);
 
        /* Write from unsigned long long src input. */
        for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
-               for (l = nrbits_ull; l < (CHAR_BIT * TEST_LEN) - s; l++) {
+               for (l = nrbits_ull; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
                        init_byte_array(target.c, TEST_LEN, 0xFF);
                        bt_bitfield_write(target.c, unsigned char, s, l, src_ull);
                        bt_bitfield_read(target.c, unsigned char, s, l, &readval);
@@ -271,11 +273,12 @@ void run_test_unsigned_read(unsigned int src_ui, unsigned long long src_ull)
        unsigned long long readval_ull;
        unsigned int s, l;
 
+       /* The number of bits needed to represent 0 is 0. */
        nrbits_ui = fls_u32(src_ui);
 
        /* Read to unsigned integer readval output. */
        for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
-               for (l = nrbits_ui; l < (CHAR_BIT * TEST_LEN) - s; l++) {
+               for (l = nrbits_ui; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
                        init_byte_array(target.c, TEST_LEN, 0xFF);
                        bt_bitfield_write(target.c, unsigned char, s, l, src_ui);
                        bt_bitfield_read(target.c, unsigned char, s, l, &readval_ui);
@@ -285,32 +288,32 @@ void run_test_unsigned_read(unsigned int src_ui, unsigned long long src_ull)
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.s, unsigned char, s, l, src_ui);
-                       bt_bitfield_read(target.c, unsigned short, s, l, &readval_ui);
+                       bt_bitfield_write(target.c, unsigned char, s, l, src_ui);
+                       bt_bitfield_read(target.s, unsigned short, s, l, &readval_ui);
                        if (check_result(src_ui, readval_ui, target.c, unsigned short,
                                        s, l, UNSIGNED_INT_READ_TEST_DESC_FMT_STR)) {
                                return;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.i, unsigned char, s, l, src_ui);
-                       bt_bitfield_read(target.c, unsigned int, s, l, &readval_ui);
+                       bt_bitfield_write(target.c, unsigned char, s, l, src_ui);
+                       bt_bitfield_read(target.i, unsigned int, s, l, &readval_ui);
                        if (check_result(src_ui, readval_ui, target.c, unsigned int,
                                        s, l, UNSIGNED_INT_READ_TEST_DESC_FMT_STR)) {
                                return;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.l, unsigned char, s, l, src_ui);
-                       bt_bitfield_read(target.c, unsigned long, s, l, &readval_ui);
+                       bt_bitfield_write(target.c, unsigned char, s, l, src_ui);
+                       bt_bitfield_read(target.l, unsigned long, s, l, &readval_ui);
                        if (check_result(src_ui, readval_ui, target.c, unsigned long,
                                        s, l, UNSIGNED_INT_READ_TEST_DESC_FMT_STR)) {
                                return;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.ll, unsigned char, s, l, src_ui);
-                       bt_bitfield_read(target.c, unsigned long long, s, l, &readval_ui);
+                       bt_bitfield_write(target.c, unsigned char, s, l, src_ui);
+                       bt_bitfield_read(target.ll, unsigned long long, s, l, &readval_ui);
                        if (check_result(src_ui, readval_ui, target.c, unsigned long long,
                                        s, l, UNSIGNED_INT_READ_TEST_DESC_FMT_STR)) {
                                return;
@@ -319,11 +322,12 @@ void run_test_unsigned_read(unsigned int src_ui, unsigned long long src_ull)
        }
        pass(UNSIGNED_INT_READ_TEST_DESC_FMT_STR, src_ui);
 
+       /* The number of bits needed to represent 0 is 0. */
        nrbits_ull = fls_u64(src_ull);
 
        /* Read to unsigned long long readval output. */
        for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
-               for (l = nrbits_ull; l < (CHAR_BIT * TEST_LEN) - s; l++) {
+               for (l = nrbits_ull; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
                        init_byte_array(target.c, TEST_LEN, 0xFF);
                        bt_bitfield_write(target.c, unsigned char, s, l, src_ull);
                        bt_bitfield_read(target.c, unsigned char, s, l, &readval_ull);
@@ -333,32 +337,32 @@ void run_test_unsigned_read(unsigned int src_ui, unsigned long long src_ull)
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.s, unsigned char, s, l, src_ull);
-                       bt_bitfield_read(target.c, unsigned short, s, l, &readval_ull);
+                       bt_bitfield_write(target.c, unsigned char, s, l, src_ull);
+                       bt_bitfield_read(target.s, unsigned short, s, l, &readval_ull);
                        if (check_result(src_ull, readval_ull, target.c, unsigned short,
                                        s, l, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
                                return;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.i, unsigned char, s, l, src_ull);
-                       bt_bitfield_read(target.c, unsigned int, s, l, &readval_ull);
+                       bt_bitfield_write(target.c, unsigned char, s, l, src_ull);
+                       bt_bitfield_read(target.i, unsigned int, s, l, &readval_ull);
                        if (check_result(src_ull, readval_ull, target.c, unsigned int,
                                        s, l, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
                                return;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.l, unsigned char, s, l, src_ull);
-                       bt_bitfield_read(target.c, unsigned long, s, l, &readval_ull);
+                       bt_bitfield_write(target.c, unsigned char, s, l, src_ull);
+                       bt_bitfield_read(target.l, unsigned long, s, l, &readval_ull);
                        if (check_result(src_ull, readval_ull, target.c, unsigned long,
                                        s, l, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
                                return;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.ll, unsigned char, s, l, src_ull);
-                       bt_bitfield_read(target.c, unsigned long long, s, l, &readval_ull);
+                       bt_bitfield_write(target.c, unsigned char, s, l, src_ull);
+                       bt_bitfield_read(target.ll, unsigned long long, s, l, &readval_ull);
                        if (check_result(src_ull, readval_ull, target.c, unsigned long long,
                                        s, l, UNSIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
                                return;
@@ -376,7 +380,7 @@ void run_test_unsigned(unsigned int src_ui, unsigned long long src_ull)
 
 void run_test_signed_write(int src_i, long long src_ll)
 {
-       int nrbits_i, nrbits_ll;
+       unsigned int nrbits_i, nrbits_ll;
        union {
                signed char c[TEST_LEN];
                short s[TEST_LEN/sizeof(short)];
@@ -387,14 +391,16 @@ void run_test_signed_write(int src_i, long long src_ll)
        long long readval;
        unsigned int s, l;
 
-       if (src_i & 0x80000000U)
+       if (!src_i)
+               nrbits_i = 0;                   /* The number of bits needed to represent 0 is 0. */
+       else if (src_i & 0x80000000U)
                nrbits_i = fls_u32(~src_i) + 1; /* Find least significant bit conveying sign */
        else
                nrbits_i = fls_u32(src_i) + 1;  /* Keep sign at 0 */
 
        /* Write from signed integer src input. */
        for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
-               for (l = nrbits_i; l < (CHAR_BIT * TEST_LEN) - s; l++) {
+               for (l = nrbits_i; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
                        init_byte_array(target.c, TEST_LEN, 0x0);
                        bt_bitfield_write(target.c, signed char, s, l, src_i);
                        bt_bitfield_read(target.c, signed char, s, l, &readval);
@@ -438,14 +444,16 @@ void run_test_signed_write(int src_i, long long src_ll)
        }
        pass(SIGNED_INT_WRITE_TEST_DESC_FMT_STR, src_i);
 
-       if (src_ll & 0x8000000000000000ULL)
+       if (!src_ll)
+               nrbits_ll = 0;                          /* The number of bits needed to represent 0 is 0. */
+       else if (src_ll & 0x8000000000000000ULL)
                nrbits_ll = fls_u64(~src_ll) + 1;       /* Find least significant bit conveying sign */
        else
                nrbits_ll = fls_u64(src_ll) + 1;        /* Keep sign at 0 */
 
        /* Write from signed long long src input. */
        for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
-               for (l = nrbits_ll; l < (CHAR_BIT * TEST_LEN) - s; l++) {
+               for (l = nrbits_ll; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
                        init_byte_array(target.c, TEST_LEN, 0x0);
                        bt_bitfield_write(target.c, signed char, s, l, src_ll);
                        bt_bitfield_read(target.c, signed char, s, l, &readval);
@@ -492,7 +500,8 @@ void run_test_signed_write(int src_i, long long src_ll)
 
 void run_test_signed_read(int src_i, long long src_ll)
 {
-       int nrbits_i, nrbits_ll, readval_i;
+       unsigned int nrbits_i, nrbits_ll;
+       int readval_i;
        union {
                unsigned char c[TEST_LEN];
                unsigned short s[TEST_LEN/sizeof(unsigned short)];
@@ -503,14 +512,16 @@ void run_test_signed_read(int src_i, long long src_ll)
        long long readval_ll;
        unsigned int s, l;
 
-       if (src_i & 0x80000000U)
+       if (!src_i)
+               nrbits_i = 0;                   /* The number of bits needed to represent 0 is 0. */
+       else if (src_i & 0x80000000U)
                nrbits_i = fls_u32(~src_i) + 1; /* Find least significant bit conveying sign */
        else
                nrbits_i = fls_u32(src_i) + 1;  /* Keep sign at 0 */
 
        /* Read to signed integer readval output. */
        for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
-               for (l = nrbits_i; l < (CHAR_BIT * TEST_LEN) - s; l++) {
+               for (l = nrbits_i; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
                        init_byte_array(target.c, TEST_LEN, 0xFF);
                        bt_bitfield_write(target.c, signed char, s, l, src_i);
                        bt_bitfield_read(target.c, signed char, s, l, &readval_i);
@@ -520,32 +531,32 @@ void run_test_signed_read(int src_i, long long src_ll)
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.s, signed char, s, l, src_i);
-                       bt_bitfield_read(target.c, short, s, l, &readval_i);
+                       bt_bitfield_write(target.c, signed char, s, l, src_i);
+                       bt_bitfield_read(target.s, short, s, l, &readval_i);
                        if (check_result(src_i, readval_i, target.c, short,
                                        s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR)) {
                                return;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.i, signed char, s, l, src_i);
-                       bt_bitfield_read(target.c, int, s, l, &readval_i);
+                       bt_bitfield_write(target.c, signed char, s, l, src_i);
+                       bt_bitfield_read(target.i, int, s, l, &readval_i);
                        if (check_result(src_i, readval_i, target.c, int,
                                        s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR)) {
                                return;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.l, signed char, s, l, src_i);
-                       bt_bitfield_read(target.c, long, s, l, &readval_i);
+                       bt_bitfield_write(target.c, signed char, s, l, src_i);
+                       bt_bitfield_read(target.l, long, s, l, &readval_i);
                        if (check_result(src_i, readval_i, target.c, long,
                                        s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR)) {
                                return;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.ll, signed char, s, l, src_i);
-                       bt_bitfield_read(target.c, long long, s, l, &readval_i);
+                       bt_bitfield_write(target.c, signed char, s, l, src_i);
+                       bt_bitfield_read(target.ll, long long, s, l, &readval_i);
                        if (check_result(src_i, readval_i, target.c, long long,
                                        s, l, SIGNED_INT_READ_TEST_DESC_FMT_STR)) {
                                return;
@@ -554,6 +565,8 @@ void run_test_signed_read(int src_i, long long src_ll)
        }
        pass(SIGNED_INT_READ_TEST_DESC_FMT_STR, src_i);
 
+       if (!src_ll)
+               nrbits_ll = 0;                          /* The number of bits needed to represent 0 is 0. */
        if (src_ll & 0x8000000000000000ULL)
                nrbits_ll = fls_u64(~src_ll) + 1;       /* Find least significant bit conveying sign */
        else
@@ -561,7 +574,7 @@ void run_test_signed_read(int src_i, long long src_ll)
 
        /* Read to signed long long readval output. */
        for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
-               for (l = nrbits_ll; l < (CHAR_BIT * TEST_LEN) - s; l++) {
+               for (l = nrbits_ll; l <= (CHAR_BIT * TEST_LEN) - s; l++) {
                        init_byte_array(target.c, TEST_LEN, 0xFF);
                        bt_bitfield_write(target.c, signed char, s, l, src_ll);
                        bt_bitfield_read(target.c, signed char, s, l, &readval_ll);
@@ -571,32 +584,32 @@ void run_test_signed_read(int src_i, long long src_ll)
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.s, signed char, s, l, src_ll);
-                       bt_bitfield_read(target.c, short, s, l, &readval_ll);
+                       bt_bitfield_write(target.c, signed char, s, l, src_ll);
+                       bt_bitfield_read(target.s, short, s, l, &readval_ll);
                        if (check_result(src_ll, readval_ll, target.c, short,
                                        s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
                                return;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.i, signed char, s, l, src_ll);
-                       bt_bitfield_read(target.c, int, s, l, &readval_ll);
+                       bt_bitfield_write(target.c, signed char, s, l, src_ll);
+                       bt_bitfield_read(target.i, int, s, l, &readval_ll);
                        if (check_result(src_ll, readval_ll, target.c, int,
                                        s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
                                return;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.l, signed char, s, l, src_ll);
-                       bt_bitfield_read(target.c, long, s, l, &readval_ll);
+                       bt_bitfield_write(target.c, signed char, s, l, src_ll);
+                       bt_bitfield_read(target.l, long, s, l, &readval_ll);
                        if (check_result(src_ll, readval_ll, target.c, long,
                                        s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
                                return;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       bt_bitfield_write(target.ll, signed char, s, l, src_ll);
-                       bt_bitfield_read(target.c, long long, s, l, &readval_ll);
+                       bt_bitfield_write(target.c, signed char, s, l, src_ll);
+                       bt_bitfield_read(target.ll, long long, s, l, &readval_ll);
                        if (check_result(src_ll, readval_ll, target.c, long long,
                                        s, l, SIGNED_LONG_LONG_READ_TEST_DESC_FMT_STR)) {
                                return;
This page took 0.029698 seconds and 4 git commands to generate.