Fix: add mmap_base_offset to ctf_stream_pos
[babeltrace.git] / tests / test-bitfield.c
index 16b2d2e44c919fc74d14353fe33185a16a2d3ef2..3bf7568280f87908bfee63240008c36900fd12dc 100644 (file)
@@ -1,14 +1,13 @@
 /*
  * test-bitfield.c
  *
- * Common Trace Format - bitfield test program
+ * BabelTrace - bitfield test program
  *
- * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * the Free Software Foundation; under version 2 of the License.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -21,7 +20,9 @@
  */
 
 #define _GNU_SOURCE
-#include <ctf/bitfield.h>
+#include <babeltrace/bitfield.h>
+#include <time.h>
+#include <stdlib.h>
 #include <stdio.h>
 
 unsigned int glob;
@@ -32,7 +33,7 @@ unsigned int glob;
  */
 void fct(void)
 {
-       ctf_bitfield_write(&glob, 12, 15, 0x12345678);
+       bt_bitfield_write(&glob, unsigned int, 12, 15, 0x12345678);
 }
 
 /* Test array size, in bytes */
@@ -95,27 +96,25 @@ static int fls(unsigned int x)
 
 #endif
 
-static void print_byte_array(const unsigned char *c, unsigned long len)
-{
-       unsigned long i;
-
-       for (i = 0; i < len; i++) {
-               printf("0x%X", c[i]);
-               if (i != len - 1)
-                       printf(" ");
-       }
-       printf("\n");
-}
-
-static void init_byte_array(unsigned char *c,
-                           unsigned long len,
-                           unsigned char val)
-{
-       unsigned long i;
-
-       for (i = 0; i < len; i++)
-               c[i] = val;
-}
+#define print_byte_array(c, len)       \
+do {                                   \
+       unsigned long i;                \
+                                       \
+       for (i = 0; i < (len); i++) {   \
+               printf("0x%X", (c)[i]); \
+               if (i != (len) - 1)     \
+                       printf(" ");    \
+       }                               \
+       printf("\n");                   \
+} while (0)
+
+#define init_byte_array(c, len, val)   \
+do {                                   \
+       unsigned long i;                \
+                                       \
+       for (i = 0; i < (len); i++)     \
+               (c)[i] = (val);         \
+} while (0)
 
 int run_test_unsigned(void)
 {
@@ -127,7 +126,7 @@ int run_test_unsigned(void)
                unsigned long l[TEST_LEN/sizeof(unsigned long)];
                unsigned long long ll[TEST_LEN/sizeof(unsigned long long)];
        } target;
-       uint64_t readval;
+       unsigned long long readval;
        unsigned int s, l;
        int err = 0;
 
@@ -139,50 +138,50 @@ int run_test_unsigned(void)
        for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
                for (l = nrbits; l < (CHAR_BIT * TEST_LEN) - s; l++) {
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       ctf_bitfield_write(target.c, s, l, src);
-                       ctf_bitfield_read(target.c, s, l, &readval);
+                       bt_bitfield_write(target.c, unsigned char, s, l, src);
+                       bt_bitfield_read(target.c, unsigned char, s, l, &readval);
                        if (readval != src) {
-                               printf("Error (bytewise) src %lX read %llX shift %d len %d\n",
+                               printf("Error (bytewise) src %X read %llX shift %d len %d\n",
                                       src, readval, s, l);
                                print_byte_array(target.c, TEST_LEN);
                                err = 1;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       ctf_bitfield_write(target.s, s, l, src);
-                       ctf_bitfield_read(target.c, s, l, &readval);
+                       bt_bitfield_write(target.s, unsigned short, s, l, src);
+                       bt_bitfield_read(target.c, unsigned char, s, l, &readval);
                        if (readval != src) {
-                               printf("Error (shortwise) src %lX read %llX shift %d len %d\n",
+                               printf("Error (shortwise) src %X read %llX shift %d len %d\n",
                                       src, readval, s, l);
                                print_byte_array(target.c, TEST_LEN);
                                err = 1;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       ctf_bitfield_write(target.i, s, l, src);
-                       ctf_bitfield_read(target.c, s, l, &readval);
+                       bt_bitfield_write(target.i, unsigned int, s, l, src);
+                       bt_bitfield_read(target.c, unsigned char, s, l, &readval);
                        if (readval != src) {
-                               printf("Error (intwise) src %lX read %llX shift %d len %d\n",
+                               printf("Error (intwise) src %X read %llX shift %d len %d\n",
                                       src, readval, s, l);
                                print_byte_array(target.c, TEST_LEN);
                                err = 1;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       ctf_bitfield_write(target.l, s, l, src);
-                       ctf_bitfield_read(target.c, s, l, &readval);
+                       bt_bitfield_write(target.l, unsigned long, s, l, src);
+                       bt_bitfield_read(target.c, unsigned char, s, l, &readval);
                        if (readval != src) {
-                               printf("Error (longwise) src %lX read %llX shift %d len %d\n",
+                               printf("Error (longwise) src %X read %llX shift %d len %d\n",
                                       src, readval, s, l);
                                print_byte_array(target.c, TEST_LEN);
                                err = 1;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0xFF);
-                       ctf_bitfield_write(target.ll, s, l, src);
-                       ctf_bitfield_read(target.c, s, l, &readval);
+                       bt_bitfield_write(target.ll, unsigned long long, s, l, src);
+                       bt_bitfield_read(target.c, unsigned char, s, l, &readval);
                        if (readval != src) {
-                               printf("Error (longlongwise) src %lX read %llX shift %d len %d\n",
+                               printf("Error (longlongwise) src %X read %llX shift %d len %d\n",
                                       src, readval, s, l);
                                print_byte_array(target.c, TEST_LEN);
                                err = 1;
@@ -200,13 +199,13 @@ int run_test_signed(void)
 {
        int src, nrbits;
        union {
-               char c[TEST_LEN];
+               signed char c[TEST_LEN];
                short s[TEST_LEN/sizeof(short)];
                int i[TEST_LEN/sizeof(int)];
                long l[TEST_LEN/sizeof(long)];
                long long ll[TEST_LEN/sizeof(long long)];
        } target;
-       int64_t readval;
+       long long readval;
        unsigned int s, l;
        int err = 0;
 
@@ -221,50 +220,50 @@ int run_test_signed(void)
        for (s = 0; s < 8 * TEST_LEN; s++) {
                for (l = nrbits; l < (8 * TEST_LEN) - s; l++) {
                        init_byte_array(target.c, TEST_LEN, 0x0);
-                       ctf_bitfield_write(target.c, s, l, src);
-                       ctf_bitfield_read(target.c, s, l, &readval);
+                       bt_bitfield_write(target.c, signed char, s, l, src);
+                       bt_bitfield_read(target.c, signed char, s, l, &readval);
                        if (readval != src) {
-                               printf("Error (bytewise) src %lX read %llX shift %d len %d\n",
+                               printf("Error (bytewise) src %X read %llX shift %d len %d\n",
                                       src, readval, s, l);
                                print_byte_array(target.c, TEST_LEN);
                                err = 1;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0x0);
-                       ctf_bitfield_write(target.s, s, l, src);
-                       ctf_bitfield_read(target.c, s, l, &readval);
+                       bt_bitfield_write(target.s, short, s, l, src);
+                       bt_bitfield_read(target.c, signed char, s, l, &readval);
                        if (readval != src) {
-                               printf("Error (shortwise) src %lX read %llX shift %d len %d\n",
+                               printf("Error (shortwise) src %X read %llX shift %d len %d\n",
                                       src, readval, s, l);
                                print_byte_array(target.c, TEST_LEN);
                                err = 1;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0x0);
-                       ctf_bitfield_write(target.i, s, l, src);
-                       ctf_bitfield_read(target.c, s, l, &readval);
+                       bt_bitfield_write(target.i, int, s, l, src);
+                       bt_bitfield_read(target.c, signed char, s, l, &readval);
                        if (readval != src) {
-                               printf("Error (intwise) src %lX read %llX shift %d len %d\n",
+                               printf("Error (intwise) src %X read %llX shift %d len %d\n",
                                       src, readval, s, l);
                                print_byte_array(target.c, TEST_LEN);
                                err = 1;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0x0);
-                       ctf_bitfield_write(target.l, s, l, src);
-                       ctf_bitfield_read(target.c, s, l, &readval);
+                       bt_bitfield_write(target.l, long, s, l, src);
+                       bt_bitfield_read(target.c, signed char, s, l, &readval);
                        if (readval != src) {
-                               printf("Error (longwise) src %lX read %llX shift %d len %d\n",
+                               printf("Error (longwise) src %X read %llX shift %d len %d\n",
                                       src, readval, s, l);
                                print_byte_array(target.c, TEST_LEN);
                                err = 1;
                        }
 
                        init_byte_array(target.c, TEST_LEN, 0x0);
-                       ctf_bitfield_write(target.ll, s, l, src);
-                       ctf_bitfield_read(target.c, s, l, &readval);
+                       bt_bitfield_write(target.ll, long long, s, l, src);
+                       bt_bitfield_read(target.c, signed char, s, l, &readval);
                        if (readval != src) {
-                               printf("Error (longlongwise) src %lX read %llX shift %d len %d\n",
+                               printf("Error (longlongwise) src %X read %llX shift %d len %d\n",
                                       src, readval, s, l);
                                print_byte_array(target.c, TEST_LEN);
                                err = 1;
@@ -318,7 +317,7 @@ int main(int argc, char **argv)
                unsigned long l[2];
                unsigned long long ll[1];
        } target;
-       uint64_t readval;
+       unsigned long long readval;
 
        if (argc > 1)
                src = atoi(argv[1]);
@@ -335,35 +334,35 @@ int main(int argc, char **argv)
 
        target.i[0] = 0xFFFFFFFF;
        target.i[1] = 0xFFFFFFFF;
-       ctf_bitfield_write(target.c, shift, len, src);
+       bt_bitfield_write(target.c, unsigned char, shift, len, src);
        printf("bytewise\n");
        print_byte_array(target.c, 8);
 
        target.i[0] = 0xFFFFFFFF;
        target.i[1] = 0xFFFFFFFF;
-       ctf_bitfield_write(target.s, shift, len, src);
+       bt_bitfield_write(target.s, unsigned short, shift, len, src);
        printf("shortwise\n");
        print_byte_array(target.c, 8);
 
        target.i[0] = 0xFFFFFFFF;
        target.i[1] = 0xFFFFFFFF;
-       ctf_bitfield_write(target.i, shift, len, src);
+       bt_bitfield_write(target.i, unsigned int, shift, len, src);
        printf("intwise\n");
        print_byte_array(target.c, 8);
 
        target.i[0] = 0xFFFFFFFF;
        target.i[1] = 0xFFFFFFFF;
-       ctf_bitfield_write(target.l, shift, len, src);
+       bt_bitfield_write(target.l, unsigned long, shift, len, src);
        printf("longwise\n");
        print_byte_array(target.c, 8);
 
        target.i[0] = 0xFFFFFFFF;
        target.i[1] = 0xFFFFFFFF;
-       ctf_bitfield_write(target.ll, shift, len, src);
+       bt_bitfield_write(target.ll, unsigned long long, shift, len, src);
        printf("lluwise\n");
        print_byte_array(target.c, 8);
 
-       ctf_bitfield_read(target.c, shift, len, &readval);
+       bt_bitfield_read(target.c, unsigned char, shift, len, &readval);
        printf("read: %llX\n", readval);
 
        ret = run_test();
This page took 0.040373 seconds and 4 git commands to generate.