Skip to content

Commit 6212640

Browse files
author
Wei Li
committed
update test
1 parent 1f0a4ac commit 6212640

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

proxylab-handout/bytes.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ void bytes_appendn(Bytes *pstr, const char *buf, size_t buf_len)
6767
/* return bytes as a cstr */
6868
void bytes_cstr(Bytes str, char *cstr)
6969
{
70-
for (int i = 0; i < str.len; i++) {
70+
int i;
71+
for (i = 0; i < str.len; i++) {
7172
cstr[i] = str.buf[i];
7273
}
7374
cstr[str.len] = '\0';

proxylab-handout/cache.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ static lru_cache_node_t *create_node_from(
2727
pnode->value = (char *)malloc(value_len*sizeof(char));
2828
pnode->value_len = value_len;
2929
strcpy(pnode->key, key);
30-
for (int i = 0; i < value_len; i++) {
30+
int i;
31+
for (i = 0; i < value_len; i++) {
3132
pnode->value[i] = value[i];
3233
}
3334
return pnode;
@@ -86,7 +87,8 @@ static void lru_cache_raise(lru_cache_t *pcache, lru_cache_node_t *cur)
8687

8788
lru_cache_node_t *lru_cache_find(lru_cache_t *pcache, const char *key)
8889
{
89-
for (lru_cache_node_t *cur = pcache->sentinel->next;
90+
lru_cache_node_t *cur;
91+
for (cur = pcache->sentinel->next;
9092
cur != pcache->sentinel; cur = cur->next) {
9193
if (strcasecmp(cur->key, key) == 0) {
9294
lru_cache_raise(pcache, cur);

proxylab-handout/test.c

+20-11
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ void test_dstring2()
3131
Bytes str;
3232
bytes_malloc(&str);
3333
char fake[2];
34+
char c;
35+
int i;
3436
fake[1] = '\0';
35-
for (char c = 'a'; c <= 'z'; c+=1) {
36-
for (int i = 0; i < 1024; i++) {
37+
for (c = 'a'; c <= 'z'; c+=1) {
38+
for (i = 0; i < 1024; i++) {
3739
fake[0] = c;
3840
bytes_append(&str, fake);
3941
}
@@ -42,8 +44,8 @@ void test_dstring2()
4244
CHECK_EQUAL(bytes_size(str), 32*1024);
4345
CHECK_EQUAL(bytes_ref(str, bytes_length(str)),
4446
'\0');
45-
for (char c = 'a'; c <= 'z'; c+=1) {
46-
for (int i = 0; i < 1024; i++) {
47+
for (c = 'a'; c <= 'z'; c+=1) {
48+
for (i = 0; i < 1024; i++) {
4749
int idx = (c-'a');
4850
idx *= 1024;
4951
idx += i;
@@ -74,7 +76,8 @@ void test_parse_uri()
7476

7577
void print_cache(lru_cache_t *pcache)
7678
{
77-
for (lru_cache_node_t *p = pcache->sentinel->next;
79+
lru_cache_node_t *p;
80+
for (p = pcache->sentinel->next;
7881
p != pcache->sentinel; p=p->next) {
7982
printf("%c ", p->value[0]);
8083
}
@@ -88,19 +91,25 @@ void test_cache()
8891
lru_cache_init(&cache, 16);
8992

9093
char str[2];
91-
for (char c = 'a'; c < 'a'+16; c += 1) {
94+
char c;
95+
for (c = 'a'; c < 'a'+16; c += 1) {
9296
str[0] = c;
9397
str[1] = '\0';
9498
lru_cache_insert(&cache, str, str, 1);
99+
CHECK_EQUAL(cache.cache_size, c-'a'+1);
100+
CHECK_EQUAL(cache.sentinel->next->value[0],
101+
str[0]);
95102
}
96103
print_cache(&cache);
97104
// printf("%zu\n", cache.cache_size);
98105
CHECK_EQUAL(cache.cache_size, 16);
99-
str[0] = 'a' + 16;
100-
lru_cache_insert(&cache, str, str, 1);
101-
CHECK_EQUAL(cache.cache_size, 16);
102-
CHECK_EQUAL(cache.sentinel->next->value[0],
103-
'a'+16);
106+
for (c = 'a'+16; c < 'a'+26; c+=1) {
107+
str[0] = c;
108+
lru_cache_insert(&cache, str, str, 1);
109+
CHECK_EQUAL(cache.cache_size, 16);
110+
CHECK_EQUAL(cache.sentinel->next->value[0],
111+
str[0]);
112+
}
104113
print_cache(&cache);
105114
lru_cache_free(&cache);
106115
}

proxylab-handout/util.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ size_t copy_until(const char *src, size_t cur, const char *terminators,
4949

5050
void tolowercstr(char *cstr)
5151
{
52-
for (int i = 0; cstr[i] != '\0'; i+=1) {
52+
int i;
53+
for (i = 0; cstr[i] != '\0'; i+=1) {
5354
cstr[i] = tolower(cstr[i]);
5455
}
5556
}

0 commit comments

Comments
 (0)