• R/O
  • SSH
  • HTTPS

bchan: 提交


Commit MetaInfo

修訂570 (tree)
時間2013-05-16 23:22:54
作者ornse01

Log Message

add array setter and getter functions for wordarray_t.

Change Summary

差異

--- bchanf/trunk/src/coll/test_wordarray.c (revision 569)
+++ bchanf/trunk/src/coll/test_wordarray.c (revision 570)
@@ -118,6 +118,66 @@
118118 return result;
119119 }
120120
121+LOCAL UNITTEST_RESULT test_wordarray_3()
122+{
123+ wordarray_t array;
124+ W i, err;
125+ UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
126+ UW data[] = {0x1, 0x2, 0x3}, data2[] = {0x5, 0x6, 0x7}, val;
127+ W data_len = sizeof(data);
128+
129+ err = wordarray_initialize(&array);
130+ if (err < 0) {
131+ printf("wordarray_initialize error: %08x\n", err);
132+ return UNITTEST_RESULT_FAIL;
133+ }
134+
135+ for (i = 0; i < data_len; i++) {
136+ err = wordarray_pushback(&array, data[i]);
137+ if (err < 0) {
138+ printf("wordarray_pushback error: %08x", err);
139+ result = UNITTEST_RESULT_FAIL;
140+ break;
141+ }
142+ }
143+
144+ for (i = 0; i < data_len; i++) {
145+ err = wordarray_getat(&array, i, &val);
146+ if (err < 0) {
147+ printf("wordarray_getat return error: %08x", err);
148+ result = UNITTEST_RESULT_FAIL;
149+ }
150+ if (val != data[i]) {
151+ printf("wordarray_getat value error: result = %d, expected = %d\n", val, data[i]);
152+ result = UNITTEST_RESULT_FAIL;
153+ }
154+ }
155+
156+ for (i = 0; i < data_len; i++) {
157+ err = wordarray_setat(&array, i, data2[i]);
158+ if (err < 0) {
159+ printf("wordarray_getat return error: %08x", err);
160+ result = UNITTEST_RESULT_FAIL;
161+ }
162+ }
163+
164+ for (i = 0; i < data_len; i++) {
165+ err = wordarray_getat(&array, i, &val);
166+ if (err < 0) {
167+ printf("wordarray_getat return error: %08x", err);
168+ result = UNITTEST_RESULT_FAIL;
169+ }
170+ if (val != data2[i]) {
171+ printf("wordarray_getat value error: result = %d, expected = %d\n", val, data2[i]);
172+ result = UNITTEST_RESULT_FAIL;
173+ }
174+ }
175+
176+ wordarray_finalize(&array);
177+
178+ return result;
179+}
180+
121181 typedef struct {
122182 UW *init;
123183 W init_len;
@@ -518,10 +578,126 @@
518578 return test_wordarray_cursor_getUW_common(&testdata);
519579 }
520580
581+typedef struct {
582+ UW *init;
583+ W init_len;
584+ UW val;
585+ W val_at;
586+ UW *expected;
587+ W expected_len;
588+} test_wordarray_cursor_setUW;
589+
590+LOCAL UNITTEST_RESULT test_wordarray_cursor_setUW_common(test_wordarray_cursor_setUW *testdata)
591+{
592+ wordarray_t array;
593+ W i, len, err;
594+ wordarray_cursor_t cursor;
595+ UW *p;
596+ UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
597+
598+ err = wordarray_initialize(&array);
599+ if (err < 0) {
600+ printf("wordarray_initialize error: %08x\n", err);
601+ return UNITTEST_RESULT_FAIL;
602+ }
603+
604+ for (i = 0; i < testdata->init_len; i++) {
605+ err = wordarray_pushback(&array, testdata->init[i]);
606+ if (err < 0) {
607+ printf("wordarray_pushback error: %08x", err);
608+ result = UNITTEST_RESULT_FAIL;
609+ break;
610+ }
611+ }
612+
613+ wordarray_cursor_initialize(&cursor, &array);
614+
615+ err = wordarray_cursor_move(&cursor, testdata->val_at);
616+ if (err < 0) {
617+ printf("wordarray_cursor_move error: %08x", err);
618+ result = UNITTEST_RESULT_FAIL;
619+ }
620+ err = wordarray_cursor_setUW(&cursor, testdata->val);
621+ if (err < 0) {
622+ printf("wordarray_cursor_setUW error: %08x", err);
623+ result = UNITTEST_RESULT_FAIL;
624+ }
625+
626+ wordarray_cursor_finalize(&cursor);
627+
628+ p = wordarray_getbuffer(&array);
629+ len = wordarray_getlength(&array);
630+ if (len != testdata->expected_len) {
631+ printf("buffer length fail: expected = %d, result = %d\n", testdata->expected_len, len);
632+ result = UNITTEST_RESULT_FAIL;
633+ } else {
634+ if (memcmp(testdata->expected, p, testdata->expected_len) != 0) {
635+ printf("buffer result fail\n");
636+ result = UNITTEST_RESULT_FAIL;
637+ for (i = 0; i < testdata->expected_len; i++) {
638+ printf("%d: %x, %x\n", i, testdata->expected[i], p[i]);
639+ }
640+ }
641+ }
642+
643+ wordarray_finalize(&array);
644+
645+ return result;
646+}
647+
648+LOCAL UNITTEST_RESULT test_wordarray_cursor_setUW_1()
649+{
650+ UW init[] = {0x1, 0x2, 0x3};
651+ W init_len = sizeof(init);
652+ UW val = 0x4;
653+ W val_at = 0;
654+ UW expected[] = {0x4, 0x2, 0x3};
655+ W expected_len = sizeof(expected);
656+ test_wordarray_cursor_setUW testdata = {
657+ init, init_len,
658+ val, val_at,
659+ expected, expected_len
660+ };
661+ return test_wordarray_cursor_setUW_common(&testdata);
662+}
663+
664+LOCAL UNITTEST_RESULT test_wordarray_cursor_setUW_2()
665+{
666+ UW init[] = {0x1, 0x2, 0x3};
667+ W init_len = sizeof(init);
668+ UW val = 0x4;
669+ W val_at = 1;
670+ UW expected[] = {0x1, 0x4, 0x3};
671+ W expected_len = sizeof(expected);
672+ test_wordarray_cursor_setUW testdata = {
673+ init, init_len,
674+ val, val_at,
675+ expected, expected_len
676+ };
677+ return test_wordarray_cursor_setUW_common(&testdata);
678+}
679+
680+LOCAL UNITTEST_RESULT test_wordarray_cursor_setUW_3()
681+{
682+ UW init[] = {0x1, 0x2, 0x3};
683+ W init_len = sizeof(init);
684+ UW val = 0x4;
685+ W val_at = 2;
686+ UW expected[] = {0x1, 0x2, 0x4};
687+ W expected_len = sizeof(expected);
688+ test_wordarray_cursor_setUW testdata = {
689+ init, init_len,
690+ val, val_at,
691+ expected, expected_len
692+ };
693+ return test_wordarray_cursor_setUW_common(&testdata);
694+}
695+
521696 EXPORT VOID test_wordarray_main(unittest_driver_t *driver)
522697 {
523698 UNITTEST_DRIVER_REGIST(driver, test_wordarray_1);
524699 UNITTEST_DRIVER_REGIST(driver, test_wordarray_2);
700+ UNITTEST_DRIVER_REGIST(driver, test_wordarray_3);
525701 UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_insert_1);
526702 UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_insert_2);
527703 UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_insert_3);
@@ -535,4 +711,7 @@
535711 UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_8);
536712 UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_erase_9);
537713 UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_getUW_1);
714+ UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_setUW_1);
715+ UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_setUW_2);
716+ UNITTEST_DRIVER_REGIST(driver, test_wordarray_cursor_setUW_3);
538717 }
--- bchanf/trunk/src/coll/wordarray.c (revision 569)
+++ bchanf/trunk/src/coll/wordarray.c (revision 570)
@@ -89,6 +89,38 @@
8989 return 0;
9090 }
9191
92+EXPORT W wordarray_getat(wordarray_t *wordarray, W at, UW *p)
93+{
94+ bytearray_cursor_t cursor;
95+ W err;
96+
97+ /* should be more efficient? */
98+ bytearray_cursor_initialize(&cursor, &wordarray->rawdata);
99+ err = bytearray_cursor_move(&cursor, at*sizeof(UW));
100+ if (err == 0) {
101+ err = bytearray_cursor_getUW(&cursor, p);
102+ }
103+ bytearray_cursor_finalize(&cursor);
104+
105+ return err;
106+}
107+
108+EXPORT W wordarray_setat(wordarray_t *wordarray, W at, UW val)
109+{
110+ bytearray_cursor_t cursor;
111+ W err;
112+
113+ /* should be more efficient? */
114+ bytearray_cursor_initialize(&cursor, &wordarray->rawdata);
115+ err = bytearray_cursor_move(&cursor, at*sizeof(UW));
116+ if (err == 0) {
117+ err = bytearray_cursor_setUW(&cursor, val);
118+ }
119+ bytearray_cursor_finalize(&cursor);
120+
121+ return err;
122+}
123+
92124 EXPORT W wordarray_initialize(wordarray_t *wordarray)
93125 {
94126 return bytearray_initialize(&wordarray->rawdata);
@@ -129,6 +161,16 @@
129161 return bytearray_cursor_getUW(&cursor->base, p);
130162 }
131163
164+EXPORT W wordarray_cursor_setW(wordarray_cursor_t *cursor, W val)
165+{
166+ return bytearray_cursor_setW(&cursor->base, val);
167+}
168+
169+EXPORT W wordarray_cursor_setUW(wordarray_cursor_t *cursor, UW val)
170+{
171+ return bytearray_cursor_setUW(&cursor->base, val);
172+}
173+
132174 EXPORT VOID wordarray_cursor_initialize(wordarray_cursor_t *cursor, wordarray_t *wordarray)
133175 {
134176 bytearray_cursor_initialize(&cursor->base, &wordarray->rawdata);
--- bchanf/trunk/src/coll/wordarray.h (revision 569)
+++ bchanf/trunk/src/coll/wordarray.h (revision 570)
@@ -55,6 +55,8 @@
5555 IMPORT W wordarray_getlength(wordarray_t *wordarray);
5656 IMPORT W wordarray_pushback(wordarray_t *wordarray, UW val);
5757 IMPORT W wordarray_popback(wordarray_t *wordarray);
58+IMPORT W wordarray_getat(wordarray_t *wordarray, W at, UW *p);
59+IMPORT W wordarray_setat(wordarray_t *wordarray, W at, UW val);
5860
5961 IMPORT VOID wordarray_cursor_initialize(wordarray_cursor_t *cursor, wordarray_t *wordarray);
6062 IMPORT VOID wordarray_cursor_finalize(wordarray_cursor_t *cursor);
@@ -64,5 +66,7 @@
6466 IMPORT Bool wordarray_cursor_isend(wordarray_cursor_t *cursor);
6567 IMPORT W wordarray_cursor_getW(wordarray_cursor_t *cursor, W *p);
6668 IMPORT W wordarray_cursor_getUW(wordarray_cursor_t *cursor, UW *p);
69+IMPORT W wordarray_cursor_setW(wordarray_cursor_t *cursor, W val);
70+IMPORT W wordarray_cursor_setUW(wordarray_cursor_t *cursor, UW val);
6771
6872 #endif
Show on old repository browser