• R/O
  • SSH
  • HTTPS

cstl: Commit


Commit MetaInfo

Revision403 (tree)
Time2010-08-16 22:43:04
Authorkatono

Log Message

LibcImpl_sprintfを実装。

Change Summary

Incremental Difference

--- branches/try-vfunc/unittest/LibcImpl.c (revision 402)
+++ branches/try-vfunc/unittest/LibcImpl.c (revision 403)
@@ -1,6 +1,16 @@
11 #include "LibcImpl.h"
22
33 #ifdef NO_STD_PRINTF
4+
5+char *LibcImpl_fgets(char *s, int size, void *stream)
6+{
7+ /* TODO */
8+ /* ex. SCI read */
9+
10+ return s;
11+}
12+
13+#ifdef STD_VSPRINTF
414 #include <stdarg.h>
515 #include <stdio.h>
616
@@ -19,14 +29,131 @@
1929 return 0;
2030 }
2131
22-char *LibcImpl_fgets(char *s, int size, void *stream)
32+#else
33+int LibcImpl_printf(const char *format, void *arg1, void *arg2, void *arg3, void *arg4)
2334 {
35+ /* NOTE: attention to buffer overflow */
36+ static char buf[1024];
37+ LibcImpl_sprintf(buf, format, arg1, arg2, arg3, arg4);
38+
2439 /* TODO */
25- /* ex. SCI read */
40+ /* ex. SCI write */
2641
27- return s;
42+ return 0;
2843 }
44+
45+static size_t dec2ascii(char *ascii, unsigned int dec, size_t width, int left_flag)
46+{
47+ size_t i;
48+ size_t ret;
49+ char tmp[16];
50+ for (i = 0; dec > 0 && i < 16; i++) {
51+ tmp[i] = dec % 10;
52+ dec /= 10;
53+ }
54+ ret = i;
55+
56+ if (width > 1) {
57+ if (left_flag) {
58+ for (i = 0; i < ret; i++) {
59+ ascii[i] = tmp[ret - i - 1] + '0';
60+ }
61+ if (width > ret) {
62+ for (i = 0; i < width - ret; i++) {
63+ ascii[ret + i] = ' ';
64+ }
65+ ret += width - ret;
66+ }
67+ } else {
68+ if (width > ret) {
69+ for (i = 0; i < width - ret; i++) {
70+ ascii[i] = ' ';
71+ }
72+ for (i = 0; i < ret; i++) {
73+ ascii[width - ret + i] = tmp[ret - i - 1] + '0';
74+ }
75+ ret += width - ret;
76+ } else {
77+ for (i = 0; i < ret; i++) {
78+ ascii[i] = tmp[ret - i - 1] + '0';
79+ }
80+ }
81+ }
82+ } else {
83+ for (i = 0; i < ret; i++) {
84+ ascii[i] = tmp[ret - i - 1] + '0';
85+ }
86+ }
87+ return ret;
88+}
89+
90+int LibcImpl_sprintf(char *buf, const char *format, void *arg1, void *arg2, void *arg3, void *arg4)
91+{
92+ size_t i;
93+ const char *p = format;
94+ void *arg_list[4];
95+ size_t arg_idx = 0;
96+ const char *tmp_str;
97+ unsigned int tmp_val;
98+ int left_flag;
99+ size_t width;
100+ size_t inc;
101+
102+ arg_list[0] = arg1;
103+ arg_list[1] = arg2;
104+ arg_list[2] = arg3;
105+ arg_list[3] = arg4;
106+
107+ i = 0;
108+ while (*p != '\0') {
109+ if (*p == '%') {
110+ p++;
111+ if (*p == '%') {
112+ buf[i++] = *(p++);
113+ } else {
114+ if (*p == '-') {
115+ left_flag = 1;
116+ p++;
117+ } else {
118+ left_flag = 0;
119+ }
120+ if ('1' <= *p && *p <= '9') {
121+ width = *p - '0';
122+ p++;
123+ } else {
124+ width = 0;
125+ }
126+ switch (*p) {
127+ case 'd':
128+ tmp_val = (unsigned int) arg_list[arg_idx];
129+ inc = dec2ascii(&buf[i], tmp_val, width, left_flag);
130+ i += inc;
131+ p++;
132+ arg_idx++;
133+ break;
134+ case 's':
135+ tmp_str = (const char *) arg_list[arg_idx];
136+ while (*tmp_str != '\0') {
137+ buf[i++] = *(tmp_str++);
138+ }
139+ p++;
140+ arg_idx++;
141+ break;
142+ default:
143+ p++;
144+ break;
145+ }
146+ }
147+ } else {
148+ buf[i++] = *(p++);
149+ }
150+ }
151+ buf[i] = '\0';
152+ return (int) i;
153+}
154+
29155 #endif
156+#endif
30157
31158 #ifdef NO_STD_MALLOC
32159 #include "UnitTest.h"
--- branches/try-vfunc/unittest/TestCase.c (revision 402)
+++ branches/try-vfunc/unittest/TestCase.c (revision 403)
@@ -37,7 +37,7 @@
3737 current_case = self;
3838 *nasserts = 0;
3939 *nasserts_failed = 0;
40- PRINTF(" Test: %s ... ", self->name);
40+ PRINTF1(" Test: %s ... ", self->name);
4141 if (SETJMP(fatal_jmp) == 0) {
4242 self->test();
4343 }
@@ -49,7 +49,7 @@
4949 failed_flag = 1;
5050 PRINTF("FAILED\n");
5151 }
52- PRINTF(" %d. %s(%d) %s\n", n, pos->file, pos->line, pos->expr);
52+ PRINTF4(" %d. %s(%d) %s\n", n, pos->file, pos->line, pos->expr);
5353 n++;
5454 }
5555 }
--- branches/try-vfunc/unittest/test/test_LibcImpl.c (revision 0)
+++ branches/try-vfunc/unittest/test/test_LibcImpl.c (revision 403)
@@ -0,0 +1,50 @@
1+#include <stdio.h>
2+#include <stdlib.h>
3+#include <string.h>
4+#include <assert.h>
5+#include "../LibcImpl.h"
6+
7+
8+void test_LibcImpl_sprintf(void)
9+{
10+ static char buf[1024];
11+ static char sp_buf[1024];
12+
13+ sprintf(sp_buf, "hogehoge");
14+ LibcImpl_sprintf(buf, "hogehoge", 0, 0, 0, 0);
15+ printf("%s\n", buf);
16+ assert(strcmp(buf, sp_buf) == 0);
17+
18+ sprintf(sp_buf, "%s", "piyopiyo");
19+ LibcImpl_sprintf(buf, "%s", "piyopiyo", 0, 0, 0);
20+ printf("%s\n", buf);
21+ assert(strcmp(buf, sp_buf) == 0);
22+
23+ sprintf(sp_buf, "foo%%bar%s", "hoge");
24+ LibcImpl_sprintf(buf, "foo%%bar%s", "hoge", 0, 0, 0);
25+ printf("%s\n", buf);
26+ assert(strcmp(buf, sp_buf) == 0);
27+
28+ sprintf(sp_buf, "%d", 9999);
29+ LibcImpl_sprintf(buf, "%d", (void *)9999, 0, 0, 0);
30+ printf("%s\n", buf);
31+ assert(strcmp(buf, sp_buf) == 0);
32+
33+ sprintf(sp_buf, "\"%d\", \"%8d\", \"%-8d\", \"%s\"", 123456789, 123456, 789, "abcd");
34+ LibcImpl_sprintf(buf, "\"%d\", \"%8d\", \"%-8d\", \"%s\"", (void *)123456789, (void *)123456, (void *)789, "abcd");
35+ printf("%s\n", buf);
36+ assert(strcmp(buf, sp_buf) == 0);
37+
38+ sprintf(sp_buf, "\"%s\", \"%9d\", \"%s\", \"%s\"", "foo", 1234567890, "bar", "abcd");
39+ LibcImpl_sprintf(buf, "\"%s\", \"%9d\", \"%s\", \"%s\"", "foo", (void *)1234567890, "bar", "abcd");
40+ printf("%s\n", buf);
41+ assert(strcmp(buf, sp_buf) == 0);
42+
43+}
44+
45+int main(void)
46+{
47+ test_LibcImpl_sprintf();
48+ return 0;
49+}
50+
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
--- branches/try-vfunc/unittest/test/Makefile (revision 0)
+++ branches/try-vfunc/unittest/test/Makefile (revision 403)
@@ -0,0 +1,26 @@
1+CFLAGS = -DNO_STD_PRINTF -Wall -ansi -pedantic-errors -g
2+PROG = unittest_test
3+OBJS = test_LibcImpl.o\
4+ ../LibcImpl.o\
5+ $(NULL)
6+
7+all: $(PROG)
8+
9+.SUFFIXES: .c .o
10+
11+.c .o:
12+ $(CC) $(CFLAGS) -c $<
13+
14+
15+$(PROG): $(OBJS)
16+ $(CC) $(CFLAGS) $(OBJS) -o $@
17+
18+
19+
20+.PHONY: clean
21+clean:
22+ rm -f *.o $(PROG) ../*.o
23+
24+
25+../LibcImpl.o: ../LibcImpl.h ../UnitTest.h
26+
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
--- branches/try-vfunc/unittest/UnitTest.c (revision 402)
+++ branches/try-vfunc/unittest/UnitTest.c (revision 403)
@@ -46,14 +46,14 @@
4646 static void print_result(void)
4747 {
4848 PRINTF("Type Total Ran Passed Failed\n");
49- PRINTF("suites %8d %8d n/a %8d", suites_total, suites_ran, suites_failed);
49+ PRINTF3("suites %8d %8d n/a %8d", suites_total, suites_ran, suites_failed);
5050 if (setup_failed || teardown_failed) {
51- PRINTF("(setup:%d/teardown:%d)\n", setup_failed, teardown_failed);
51+ PRINTF2("(setup:%d/teardown:%d)\n", setup_failed, teardown_failed);
5252 } else {
5353 PRINTF("\n");
5454 }
55- PRINTF("tests %8d %8d %8d %8d\n", tests_total, tests_ran, tests_ran - tests_failed, tests_failed);
56- PRINTF("asserts %8d %8d %8d %8d\n", asserts_total, asserts_ran, asserts_ran - asserts_failed, asserts_failed);
55+ PRINTF4("tests %8d %8d %8d %8d\n", tests_total, tests_ran, tests_ran - tests_failed, tests_failed);
56+ PRINTF4("asserts %8d %8d %8d %8d\n", asserts_total, asserts_ran, asserts_ran - asserts_failed, asserts_failed);
5757 PRINTF("\n");
5858 }
5959
@@ -135,7 +135,7 @@
135135 PRINTF("List Tests\n");
136136 PRINTF(" Number Name\n");
137137 for (i = 0; cases[i].name != 0; i++) {
138- PRINTF(" %-6d %s\n", i + 1, cases[i].name);
138+ PRINTF2(" %-6d %s\n", i + 1, cases[i].name);
139139 }
140140 PRINTF("\n");
141141 }
@@ -146,7 +146,7 @@
146146 PRINTF("List Suites\n");
147147 PRINTF(" Number Name\n");
148148 for (i = 0; suites[i].name != 0; i++) {
149- PRINTF(" %-6d %s\n", i + 1, suites[i].name);
149+ PRINTF2(" %-6d %s\n", i + 1, suites[i].name);
150150 }
151151 PRINTF("\n");
152152 }
@@ -253,7 +253,7 @@
253253
254254 PRINTF("\n");
255255 while (1) {
256- PRINTF("================== Suite : %s ==================\n", selected_suite->name);
256+ PRINTF1("================== Suite : %s ==================\n", selected_suite->name);
257257 PRINTF("(R)un all, (S)elect test, (L)ist tests, (M)ove up, (Q)uit\n");
258258 PRINTF("Enter Command : ");
259259 FGETS(input_buf, sizeof input_buf, stdin);
--- branches/try-vfunc/unittest/LibcImpl.h (revision 402)
+++ branches/try-vfunc/unittest/LibcImpl.h (revision 403)
@@ -4,7 +4,11 @@
44
55 #ifndef NO_STD_PRINTF
66 # include <stdio.h>
7-# define PRINTF printf
7+# define PRINTF printf
8+# define PRINTF1 printf
9+# define PRINTF2 printf
10+# define PRINTF3 printf
11+# define PRINTF4 printf
812 # define FGETS fgets
913 #else
1014 # ifdef stdin
@@ -11,10 +15,24 @@
1115 # undef stdin
1216 # endif
1317 # define stdin 0
14-# define PRINTF LibcImpl_printf
1518 # define FGETS LibcImpl_fgets
19+char *LibcImpl_fgets(char *s, int size, void *stream);
20+# ifdef STD_VSPRINTF
21+# define PRINTF LibcImpl_printf
22+# define PRINTF1 LibcImpl_printf
23+# define PRINTF2 LibcImpl_printf
24+# define PRINTF3 LibcImpl_printf
25+# define PRINTF4 LibcImpl_printf
1626 int LibcImpl_printf(const char *format, ...);
17-char *LibcImpl_fgets(char *s, int size, void *stream);
27+# else
28+# define PRINTF(f) LibcImpl_printf(f, 0, 0, 0, 0)
29+# define PRINTF1(f, a1) LibcImpl_printf(f, (void *)(a1), 0, 0, 0)
30+# define PRINTF2(f, a1, a2) LibcImpl_printf(f, (void *)(a1), (void *)(a2), 0, 0)
31+# define PRINTF3(f, a1, a2, a3) LibcImpl_printf(f, (void *)(a1), (void *)(a2), (void *)(a3), 0)
32+# define PRINTF4(f, a1, a2, a3, a4) LibcImpl_printf(f, (void *)(a1), (void *)(a2), (void *)(a3), (void *)(a4))
33+int LibcImpl_printf(const char *format, void *arg1, void *arg2, void *arg3, void *arg4);
34+int LibcImpl_sprintf(char *buf, const char *format, void *arg1, void *arg2, void *arg3, void *arg4);
35+# endif
1836 #endif
1937
2038
--- branches/try-vfunc/unittest/TestSuite.c (revision 402)
+++ branches/try-vfunc/unittest/TestSuite.c (revision 403)
@@ -45,10 +45,10 @@
4545 size_t i;
4646 int err;
4747 *ncases = *ncases_failed = *nasserts = *nasserts_failed = 0;
48- PRINTF("Suite: %s\n", self->name);
48+ PRINTF1("Suite: %s\n", self->name);
4949 err = TestSuite_setup(self);
5050 if (err) {
51- PRINTF(" SETUP FAILED: error[%d]\n", err);
51+ PRINTF1(" SETUP FAILED: error[%d]\n", err);
5252 PRINTF("\n");
5353 return SETUP_NG;
5454 }
@@ -64,7 +64,7 @@
6464 (*ncases) += i;
6565 err = TestSuite_teardown(self);
6666 if (err) {
67- PRINTF(" TEARDOWN FAILED: error[%d]\n", err);
67+ PRINTF1(" TEARDOWN FAILED: error[%d]\n", err);
6868 PRINTF("\n");
6969 return TEARDOWN_NG;
7070 }
@@ -77,10 +77,10 @@
7777 {
7878 int err;
7979 *ncases = *ncases_failed = *nasserts = *nasserts_failed = 0;
80- PRINTF("Suite: %s\n", self->name);
80+ PRINTF1("Suite: %s\n", self->name);
8181 err = TestSuite_setup(self);
8282 if (err) {
83- PRINTF(" SETUP FAILED: error[%d]\n", err);
83+ PRINTF1(" SETUP FAILED: error[%d]\n", err);
8484 PRINTF("\n");
8585 return SETUP_NG;
8686 }
@@ -91,7 +91,7 @@
9191 *ncases = 1;
9292 err = TestSuite_teardown(self);
9393 if (err) {
94- PRINTF(" TEARDOWN FAILED: error[%d]\n", err);
94+ PRINTF1(" TEARDOWN FAILED: error[%d]\n", err);
9595 PRINTF("\n");
9696 return TEARDOWN_NG;
9797 }
Show on old repository browser