回答編集履歴

1

test コードを引用するのを忘れてた。

2015/03/17 22:19

投稿

katoy
katoy

スコア22324

test CHANGED
@@ -7,6 +7,142 @@
7
7
 
8
8
 
9
9
  ```lang-c
10
+
11
+ #include <stdio.h>
12
+
13
+ #include <string.h>
14
+
15
+
16
+
17
+ #define MAXLINE (100)
18
+
19
+ #define TAB_STOP (8) /* タブストップは8文字ごと */
20
+
21
+
22
+
23
+ // 印字可能文字な ASCII 文字だけを扱う。
24
+
25
+ // TODO: entab の結果が MAXLINE を超える時は MAXLINE より後ろはカットする。
26
+
27
+ void entab(char * str, char* entabstr) {
28
+
29
+ // str をそのまま entabstr に copy するだけの仮実装
30
+
31
+ int len = strlen(str);
32
+
33
+ for (int i = 0; i < len; i++) {
34
+
35
+ char c = str[i];
36
+
37
+ entabstr[i] = c;
38
+
39
+ }
40
+
41
+ entabstr[len] = '\0';
42
+
43
+ }
44
+
45
+
46
+
47
+ char str_max[MAXLINE];
48
+
49
+ char str_max_1[MAXLINE + 1];
50
+
51
+ char * TEST_CASES[] = {
52
+
53
+ /* 0 */ "11223344", "11223344",
54
+
55
+ /* 1 */ "112233 ", "112233\t",
56
+
57
+ /* 2 */ "1122 44", "1122 44",
58
+
59
+ /* 3 */ " 223344", " 223344",
60
+
61
+ /* 4 */ "11 33 ", "11 33\t",
62
+
63
+
64
+
65
+ /* 5 */ "\t", "\t",
66
+
67
+ /* 6 */ " \t", "\t",
68
+
69
+ /* 7 */ "11\t", "11\t",
70
+
71
+ /* 8 */ "", "",
72
+
73
+ /* 9 */ // str_max, str_max,
74
+
75
+ /* 10 */ // str_max_1, str_max,
76
+
77
+ };
78
+
79
+ void setup_testcases() {
80
+
81
+ for (int i = 0; i < MAXLINE - 1; i++) {
82
+
83
+ str_max[i] = 'x';
84
+
85
+ }
86
+
87
+ str_max[MAXLINE - 1] = '\0';
88
+
89
+
90
+
91
+ for (int i = 0; i < MAXLINE; i++) {
92
+
93
+ str_max_1[i] = 'x';
94
+
95
+ }
96
+
97
+ str_max_1[MAXLINE] = '\0';
98
+
99
+ };
100
+
101
+
102
+
103
+ void test_entab() {
104
+
105
+
106
+
107
+ int len = sizeof(TEST_CASES) / sizeof(char *) / 2;
108
+
109
+ char entabstr[MAXLINE];
110
+
111
+
112
+
113
+ setup_testcases();
114
+
115
+ for (int i = 0; i < len; i++) {
116
+
117
+ char * str = TEST_CASES[i * 2];
118
+
119
+ char * ans = TEST_CASES[i * 2 + 1];
120
+
121
+ memset(entabstr, '*', MAXLINE); // ダミー値で埋める。
122
+
123
+ entab(str, entabstr);
124
+
125
+ if (strcmp(ans, entabstr) != 0) {
126
+
127
+ printf("#--- Error: %d:\t%s,\t[%s] != [%s]\n", i, str, ans, entabstr);
128
+
129
+ printf("#--- : \t%d,%d\n", (int)strlen(ans), (int)strlen(entabstr));
130
+
131
+ }
132
+
133
+ }
134
+
135
+ }
136
+
137
+
138
+
139
+ int main(int atgc, char** argv) {
140
+
141
+ test_entab();
142
+
143
+ return 0;
144
+
145
+ }
10
146
 
11
147
  ```
12
148
 
@@ -18,14 +154,26 @@
18
154
 
19
155
  $ ./a.out
20
156
 
157
+ katoy-no-MacBook-Pro-2:cpp katoy$ ./a.out
158
+
21
159
  #--- Error: 1: 112233 , [112233 ] != [112233 ]
160
+
161
+ #--- : 7,8
22
162
 
23
163
  #--- Error: 4: 11 33 , [11 33 ] != [11 33 ]
24
164
 
165
+ #--- : 7,8
166
+
25
167
  #--- Error: 6: , [ ] != [ ]
168
+
169
+ #--- : 1,3
26
170
 
27
171
  ```
28
172
 
29
173
  TEST_CASES の 1番、4番、6番が正しい entab 結果を返していない事が示されています。
30
174
 
31
175
  (これ以外は、entab をしても文字列内容に変化が起こらない場合のテストです)
176
+
177
+
178
+
179
+ 文字列長さが MAXLINE を超える時のテストケースは上ではコメントアウトしてますが、最終的には コメントを外します。