Skip to content

Commit cc58392

Browse files
committed
fix dstring length bug
1 parent bf0d9b1 commit cc58392

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

proxylab-handout/dstring.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void string_append(string *pstr, const char *cstr)
4545
strcpy(new_str, pstr->cstr);
4646
strcpy(&new_str[pstr->len], cstr);
4747
pstr->cstr = new_str;
48-
pstr->len = pstr->len + cstr_len + 1;
48+
pstr->len += cstr_len;
4949
pstr->size = new_size;
5050
}
5151
}

proxylab-handout/proxy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void forward_response(int infd, int outfd)
4949
string_malloc(&response);
5050

5151
while (Rio_readlineb(&rio, buf, MAXLINE)) {
52-
printf("%s\n", buf);
52+
// printf("%s\n", buf);
5353
string_append(&response, buf);
5454
}
5555
#ifdef DEBUG

proxylab-handout/test.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
#include <string.h>
77

88
#define CHECK_STREQUAL(s1, s2) if (strcmp((s1), (s2))) { \
9-
fprintf(stderr, "[CHECK_STREQUAL] %s != %s\n", s1, s2); \
9+
fprintf(stderr, "%d [CHECK_STREQUAL] %s != %s\n", __LINE__, s1, s2); \
10+
exit(-1);\
1011
}
1112

12-
#define CHECK_EQUAL(a, b) if ((a) == (b)) {\
13-
fprintf(stderr, "[CHECK_EQUAL] failed"); \
13+
#define CHECK_EQUAL(a, b) if (!((a) == (b))) {\
14+
fprintf(stderr, "%d [CHECK_EQUAL] failed", __LINE__); \
15+
fflush(stderr);\
16+
exit(-1);\
1417
}
1518

1619
void test_dstring1()
@@ -26,18 +29,26 @@ void test_dstring2()
2629
{
2730
string str;
2831
string_malloc(&str);
29-
for (int i = 0; i < 1024; i++) {
30-
string_append(&str, "a");
32+
char fake[2];
33+
fake[1] = '\0';
34+
for (char c = 'a'; c <= 'z'; c+=1) {
35+
for (int i = 0; i < 1024; i++) {
36+
fake[0] = c;
37+
string_append(&str, fake);
38+
}
3139
}
32-
string_append(&str, "b");
33-
CHECK_EQUAL(string_length(str), 1025);
34-
CHECK_EQUAL(string_size(str), 2048);
40+
CHECK_EQUAL(string_length(str), 26*1024);
41+
CHECK_EQUAL(string_size(str), 32*1024);
3542
CHECK_EQUAL(string_ref(str, string_length(str)),
3643
'\0');
37-
for (int i = 0; i < 1024; i++) {
38-
CHECK_EQUAL(string_ref(str, i), 'a');
44+
for (char c = 'a'; c <= 'z'; c+=1) {
45+
for (int i = 0; i < 1024; i++) {
46+
int idx = (c-'a');
47+
idx *= 1024;
48+
idx += i;
49+
CHECK_EQUAL(string_ref(str, idx), c);
50+
}
3951
}
40-
CHECK_EQUAL(string_ref(str, string_length(str)-1), 'b');
4152
string_free(&str);
4253
}
4354

0 commit comments

Comments
 (0)