public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
Search Repo:
Add buffer tests
macournoyer (author)
Mon Apr 07 19:57:39 -0700 2008
commit  993e78f402183c0e58a5f2c9442d8565eaa53268
tree    4326eeabebf6706a180369a79742fb92c8627c3a
parent  747a41479340d52976aa067c3e93198a40d74627
...
21
22
23
24
 
 
25
26
27
 
28
29
 
30
31
32
33
34
35
36
 
 
 
37
38
39
40
41
42
43
 
44
45
46
...
21
22
23
 
24
25
26
27
28
29
30
 
31
32
33
34
35
36
 
 
37
38
39
40
41
42
43
44
45
 
46
47
48
49
0
@@ -21,26 +21,29 @@
0
 
0
 static inline void buffer_grow(buffer_t *buf, size_t size)
0
 {
0
- char *new, *old;
0
+ char *new, *old;
0
+ size_t num = (size_t) (size + 0.5) / (float) buf->pool->size;
0
   
0
   /* TODO store big body in tempfile */
0
   /* TODO if last alloc, just alloc next block */
0
+
0
   old = buf->ptr;
0
- new = (char *) palloc(buf->pool, buf->nalloc + 1);
0
+ new = (char *) palloc(buf->pool, buf->nalloc + num);
0
   assert(new);
0
   
0
   memcpy(new, old, buf->len);
0
 
0
   buf->ptr = new;
0
- buf->nalloc ++;
0
- buf->salloc += buf->pool->size;
0
+ buf->nalloc += num;
0
+ buf->salloc += buf->pool->size * num;
0
+
0
   pfree(buf->pool, old);
0
 }
0
 
0
 void buffer_append(buffer_t *buf, const char *ptr, size_t len)
0
 {
0
   /* alloc more mem when buffer full */
0
- if (buf->len >= buf->salloc)
0
+ if (buf->len + len > buf->salloc)
0
     buffer_grow(buf, len);
0
   
0
   memcpy(buf->ptr + buf->len, ptr, len);
...
233
234
235
236
237
238
239
...
233
234
235
 
236
237
238
0
@@ -233,7 +233,6 @@
0
     VALUE headers = rb_ary_entry(response, 1);
0
     VALUE body = rb_ary_entry(response, 2);
0
     
0
- /* TODO grow buffer if too small */
0
     connection_send_status(c, status);
0
     connection_send_headers(c, headers);
0
     connection_send_body(c, body);
...
1
2
3
4
5
 
 
6
7
8
...
1
2
3
 
 
4
5
6
7
8
0
@@ -1,8 +1,8 @@
0
 CC = gcc
0
 CFLAGS = -fno-common -g -O2 -pipe -I../ext/thin_backend -DDEBUG
0
 
0
-OBJS = $(addprefix ../ext/thin_backend/,palloc.o array.o)
0
-TARGETS = $(addsuffix _test,palloc array status)
0
+OBJS = $(addprefix ../ext/thin_backend/,palloc.o array.o buffer.o)
0
+TARGETS = $(addsuffix _test,palloc array status buffer)
0
 
0
 %: %.c
0
   $(CC) $(CFLAGS) -c $<
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
0
@@ -1 +1,81 @@
0
+#include "test.h"
0
+#include "buffer.h"
0
+
0
+test_init();
0
+
0
+void test_buffer_init(void)
0
+{
0
+ pool_t *p = pool_create(10, 512);
0
+ buffer_t b;
0
+
0
+ buffer_init(&b, p);
0
+
0
+ assert_equal(p, b.pool);
0
+ assert_equal(1, b.nalloc);
0
+ assert_equal(512, b.salloc);
0
+
0
+ pool_destroy(p);
0
+}
0
+
0
+void test_buffer_append(void)
0
+{
0
+ pool_t *p = pool_create(10, 1024);
0
+ buffer_t b;
0
+
0
+ buffer_init(&b, p);
0
+
0
+ buffer_append(&b, "hi", 2);
0
+ assert_str_equal("hi", b.ptr);
0
+
0
+ buffer_append(&b, " you", 4);
0
+ assert_str_equal("hi you", b.ptr);
0
+ assert_equal(6, b.len);
0
+ assert_equal(1, b.nalloc);
0
+
0
+ pool_destroy(p);
0
+}
0
+
0
+void test_buffer_grow_and_append(void)
0
+{
0
+ pool_t *p = pool_create(10, 2);
0
+ buffer_t b;
0
+
0
+ buffer_init(&b, p);
0
+
0
+ buffer_append(&b, "hi", 2);
0
+ assert_equal(1, b.nalloc);
0
+ assert_equal(2, b.salloc);
0
+
0
+ buffer_append(&b, " you", 4);
0
+ assert_equal(6, b.len);
0
+ assert_equal(3, b.nalloc);
0
+ assert_equal(6, b.salloc);
0
+
0
+ assert_str_equal("hi you", b.ptr);
0
+
0
+ pool_destroy(p);
0
+}
0
+
0
+void test_buffer_free(void)
0
+{
0
+ pool_t *p = pool_create(10, 2);
0
+ buffer_t b;
0
+
0
+ buffer_init(&b, p);
0
+ buffer_free(&b);
0
+
0
+ pool_destroy(p);
0
+}
0
+
0
+int main(int argc, char const *argv[])
0
+{
0
+ test_start();
0
+
0
+ test_buffer_init();
0
+ test_buffer_append();
0
+ test_buffer_grow_and_append();
0
+ test_buffer_free();
0
+
0
+ test_end();
0
+}
...
5
6
7
8
9
10
11
 
 
 
 
12
13
14
15
16
 
17
18
19
...
5
6
7
 
 
 
 
8
9
10
11
12
13
14
15
 
16
17
18
19
0
@@ -5,15 +5,15 @@
0
 
0
 void test_get_status(void)
0
 {
0
- assert_equal("100 Continue", thin_status(100));
0
- assert_equal("200 OK", thin_status(200));
0
- assert_equal("500 Internal Server Error", thin_status(500));
0
- assert_equal("405 Method Not Allowed", thin_status(405));
0
+ assert_equal("100 Continue", get_status_line(100));
0
+ assert_equal("200 OK", get_status_line(200));
0
+ assert_equal("500 Internal Server Error", get_status_line(500));
0
+ assert_equal("405 Method Not Allowed", get_status_line(405));
0
 }
0
 
0
 void test_unknow_status_return_200_ok(void)
0
 {
0
- assert_equal("200 OK", thin_status(999));
0
+ assert_equal("200 OK", get_status_line(999));
0
 }
0
 
0
 int main(int argc, char const *argv[])
...
17
18
19
 
 
 
 
 
 
 
 
...
17
18
19
20
21
22
23
24
25
26
27
0
@@ -17,4 +17,12 @@
0
     printf(#expected " expected but was " #actual ", at line %d\n", __LINE__); \
0
     failures++; \
0
   }
0
+
0
+#define assert_str_equal(expected, actual) \
0
+ assertions++; \
0
+ if (strcmp(expected, actual) != 0) { \
0
+ printf("%s expected but was %s, at line %d\n", expected, actual, __LINE__); \
0
+ failures++; \
0
+ }
0
+

Comments

    No one has commented yet.