OSDN Git Service

add array setter and getter functions for wordarray_t.
authorornse01 <ornse01@users.sourceforge.jp>
Thu, 16 May 2013 14:22:54 +0000 (14:22 +0000)
committerornse01 <ornse01@users.sourceforge.jp>
Thu, 16 May 2013 14:22:54 +0000 (14:22 +0000)
git-svn-id: http://svn.sourceforge.jp/svnroot/bchan/bchanf/trunk@570 20a0b8eb-f62a-4a12-8fe1-b598822500fb

src/coll/test_wordarray.c
src/coll/wordarray.c
src/coll/wordarray.h

index a363ee2..db17156 100644 (file)
@@ -118,6 +118,66 @@ LOCAL UNITTEST_RESULT test_wordarray_2()
        return result;
 }
 
+LOCAL UNITTEST_RESULT test_wordarray_3()
+{
+       wordarray_t array;
+       W i, err;
+       UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
+       UW data[] = {0x1, 0x2, 0x3}, data2[] = {0x5, 0x6, 0x7}, val;
+       W data_len = sizeof(data);
+
+       err = wordarray_initialize(&array);
+       if (err < 0) {
+               printf("wordarray_initialize error: %08x\n", err);
+               return UNITTEST_RESULT_FAIL;
+       }
+
+       for (i = 0; i < data_len; i++) {
+               err = wordarray_pushback(&array, data[i]);
+               if (err < 0) {
+                       printf("wordarray_pushback error: %08x", err);
+                       result = UNITTEST_RESULT_FAIL;
+                       break;
+               }
+       }
+
+       for (i = 0; i < data_len; i++) {
+               err = wordarray_getat(&array, i, &val);
+               if (err < 0) {
+                       printf("wordarray_getat return error: %08x", err);
+                       result = UNITTEST_RESULT_FAIL;
+               }
+               if (val != data[i]) {
+                       printf("wordarray_getat value error: result = %d, expected = %d\n", val, data[i]);
+                       result = UNITTEST_RESULT_FAIL;
+               }
+       }
+
+       for (i = 0; i < data_len; i++) {
+               err = wordarray_setat(&array, i, data2[i]);
+               if (err < 0) {
+                       printf("wordarray_getat return error: %08x", err);
+                       result = UNITTEST_RESULT_FAIL;
+               }
+       }
+
+       for (i = 0; i < data_len; i++) {
+               err = wordarray_getat(&array, i, &val);
+               if (err < 0) {
+                       printf("wordarray_getat return error: %08x", err);
+                       result = UNITTEST_RESULT_FAIL;
+               }
+               if (val != data2[i]) {
+                       printf("wordarray_getat value error: result = %d, expected = %d\n", val, data2[i]);
+                       result = UNITTEST_RESULT_FAIL;
+               }
+       }
+
+       wordarray_finalize(&array);
+
+       return result;
+}
+
 typedef struct {
        UW *init;
        W init_len;
@@ -518,10 +578,126 @@ LOCAL UNITTEST_RESULT test_wordarray_cursor_getUW_1()
        return test_wordarray_cursor_getUW_common(&testdata);
 }
 
+typedef struct {
+       UW *init;
+       W init_len;
+       UW val;
+       W val_at;
+       UW *expected;
+       W expected_len;
+} test_wordarray_cursor_setUW;
+
+LOCAL UNITTEST_RESULT test_wordarray_cursor_setUW_common(test_wordarray_cursor_setUW *testdata)
+{
+       wordarray_t array;
+       W i, len, err;
+       wordarray_cursor_t cursor;
+       UW *p;
+       UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
+
+       err = wordarray_initialize(&array);
+       if (err < 0) {
+               printf("wordarray_initialize error: %08x\n", err);
+               return UNITTEST_RESULT_FAIL;
+       }
+
+       for (i = 0; i < testdata->init_len; i++) {
+               err = wordarray_pushback(&array, testdata->init[i]);
+               if (err < 0) {
+                       printf("wordarray_pushback error: %08x", err);
+                       result = UNITTEST_RESULT_FAIL;
+                       break;
+               }
+       }
+
+       wordarray_cursor_initialize(&cursor, &array);
+
+       err = wordarray_cursor_move(&cursor, testdata->val_at);
+       if (err < 0) {
+               printf("wordarray_cursor_move error: %08x", err);
+               result = UNITTEST_RESULT_FAIL;
+       }
+       err = wordarray_cursor_setUW(&cursor, testdata->val);
+       if (err < 0) {
+               printf("wordarray_cursor_setUW error: %08x", err);
+               result = UNITTEST_RESULT_FAIL;
+       }
+
+       wordarray_cursor_finalize(&cursor);
+
+       p = wordarray_getbuffer(&array);
+       len = wordarray_getlength(&array);
+       if (len != testdata->expected_len) {
+               printf("buffer length fail: expected = %d, result = %d\n", testdata->expected_len, len);
+               result = UNITTEST_RESULT_FAIL;
+       } else {
+               if (memcmp(testdata->expected, p, testdata->expected_len) != 0) {
+                       printf("buffer result fail\n");
+                       result = UNITTEST_RESULT_FAIL;
+                       for (i = 0; i < testdata->expected_len; i++) {
+                               printf("%d: %x, %x\n", i, testdata->expected[i], p[i]);
+                       }
+               }
+       }
+
+       wordarray_finalize(&array);
+
+       return result;
+}
+
+LOCAL UNITTEST_RESULT test_wordarray_cursor_setUW_1()
+{
+       UW init[] = {0x1, 0x2, 0x3};
+       W init_len = sizeof(init);
+       UW val = 0x4;
+       W val_at = 0;
+       UW expected[] = {0x4, 0x2, 0x3};
+       W expected_len = sizeof(expected);
+       test_wordarray_cursor_setUW testdata = {
+               init, init_len,
+               val, val_at,
+               expected, expected_len
+       };
+       return test_wordarray_cursor_setUW_common(&testdata);
+}
+
+LOCAL UNITTEST_RESULT test_wordarray_cursor_setUW_2()
+{
+       UW init[] = {0x1, 0x2, 0x3};
+       W init_len = sizeof(init);
+       UW val = 0x4;
+       W val_at = 1;
+       UW expected[] = {0x1, 0x4, 0x3};
+       W expected_len = sizeof(expected);
+       test_wordarray_cursor_setUW testdata = {
+               init, init_len,
+               val, val_at,
+               expected, expected_len
+       };
+       return test_wordarray_cursor_setUW_common(&testdata);
+}
+
+LOCAL UNITTEST_RESULT test_wordarray_cursor_setUW_3()
+{
+       UW init[] = {0x1, 0x2, 0x3};
+       W init_len = sizeof(init);
+       UW val = 0x4;
+       W val_at = 2;
+       UW expected[] = {0x1, 0x2, 0x4};
+       W expected_len = sizeof(expected);
+       test_wordarray_cursor_setUW testdata = {
+               init, init_len,
+               val, val_at,
+               expected, expected_len
+       };
+       return test_wordarray_cursor_setUW_common(&testdata);
+}
+
 EXPORT VOID test_wordarray_main(unittest_driver_t *driver)
 {
        UNITTEST_DRIVER_REGIST(driver, test_wordarray_1);
        UNITTEST_DRIVER_REGIST(driver, test_wordarray_2);
+       UNITTEST_DRIVER_REGIST(driver, test_wordarray_3);
        UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_insert_1);
        UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_insert_2);
        UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_insert_3);
@@ -535,4 +711,7 @@ EXPORT VOID test_wordarray_main(unittest_driver_t *driver)
        UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_8);
        UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_9);
        UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_getUW_1);
+       UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_setUW_1);
+       UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_setUW_2);
+       UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_setUW_3);
 }
index d673401..1cd01e5 100644 (file)
@@ -89,6 +89,38 @@ EXPORT W wordarray_popback(wordarray_t *wordarray)
        return 0;
 }
 
+EXPORT W wordarray_getat(wordarray_t *wordarray, W at, UW *p)
+{
+       bytearray_cursor_t cursor;
+       W err;
+
+       /* should be more efficient? */
+       bytearray_cursor_initialize(&cursor, &wordarray->rawdata);
+       err = bytearray_cursor_move(&cursor, at*sizeof(UW));
+       if (err == 0) {
+               err = bytearray_cursor_getUW(&cursor, p);
+       }
+       bytearray_cursor_finalize(&cursor);
+
+       return err;
+}
+
+EXPORT W wordarray_setat(wordarray_t *wordarray, W at, UW val)
+{
+       bytearray_cursor_t cursor;
+       W err;
+
+       /* should be more efficient? */
+       bytearray_cursor_initialize(&cursor, &wordarray->rawdata);
+       err = bytearray_cursor_move(&cursor, at*sizeof(UW));
+       if (err == 0) {
+               err = bytearray_cursor_setUW(&cursor, val);
+       }
+       bytearray_cursor_finalize(&cursor);
+
+       return err;
+}
+
 EXPORT W wordarray_initialize(wordarray_t *wordarray)
 {
        return bytearray_initialize(&wordarray->rawdata);
@@ -129,6 +161,16 @@ EXPORT W wordarray_cursor_getUW(wordarray_cursor_t *cursor, UW *p)
        return bytearray_cursor_getUW(&cursor->base, p);
 }
 
+EXPORT W wordarray_cursor_setW(wordarray_cursor_t *cursor, W val)
+{
+       return bytearray_cursor_setW(&cursor->base, val);
+}
+
+EXPORT W wordarray_cursor_setUW(wordarray_cursor_t *cursor, UW val)
+{
+       return bytearray_cursor_setUW(&cursor->base, val);
+}
+
 EXPORT VOID wordarray_cursor_initialize(wordarray_cursor_t *cursor, wordarray_t *wordarray)
 {
        bytearray_cursor_initialize(&cursor->base, &wordarray->rawdata);
index d297b94..a9023db 100644 (file)
@@ -55,6 +55,8 @@ IMPORT UW* wordarray_getbuffer(wordarray_t *wordarray);
 IMPORT W wordarray_getlength(wordarray_t *wordarray);
 IMPORT W wordarray_pushback(wordarray_t *wordarray, UW val);
 IMPORT W wordarray_popback(wordarray_t *wordarray);
+IMPORT W wordarray_getat(wordarray_t *wordarray, W at, UW *p);
+IMPORT W wordarray_setat(wordarray_t *wordarray, W at, UW val);
 
 IMPORT VOID wordarray_cursor_initialize(wordarray_cursor_t *cursor, wordarray_t *wordarray);
 IMPORT VOID wordarray_cursor_finalize(wordarray_cursor_t *cursor);
@@ -64,5 +66,7 @@ IMPORT W wordarray_cursor_insert(wordarray_cursor_t *cursor, UW *data, W len);
 IMPORT Bool wordarray_cursor_isend(wordarray_cursor_t *cursor);
 IMPORT W wordarray_cursor_getW(wordarray_cursor_t *cursor, W *p);
 IMPORT W wordarray_cursor_getUW(wordarray_cursor_t *cursor, UW *p);
+IMPORT W wordarray_cursor_setW(wordarray_cursor_t *cursor, W val);
+IMPORT W wordarray_cursor_setUW(wordarray_cursor_t *cursor, UW val);
 
 #endif