/[jscoverage]/trunk/tests/streams.c
ViewVC logotype

Annotation of /trunk/tests/streams.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 505 - (hide annotations)
Sat Jan 2 04:55:54 2010 UTC (11 years ago) by siliconforks
File MIME type: text/plain
File size: 3104 byte(s)
Update copyright year.

1 siliconforks 93 /*
2     streams.c - test `Stream' object
3 siliconforks 505 Copyright (C) 2008, 2009, 2010 siliconforks.com
4 siliconforks 93
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9    
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     GNU General Public License for more details.
14    
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18     */
19    
20     #include <assert.h>
21     #include <string.h>
22    
23     #include "stream.h"
24 siliconforks 230 #include "util.h"
25 siliconforks 93
26     int main(void) {
27     Stream * stream;
28    
29     stream = Stream_new(0);
30     Stream_write_string(stream, "a");
31     Stream_write_string(stream, "bc");
32     assert(stream->length == 3);
33     assert(memcmp(stream->data, "abc", 3) == 0);
34     Stream_delete(stream);
35    
36 siliconforks 230 /* test writing chars to stream */
37     stream = Stream_new(2);
38     Stream_write_char(stream, 'a');
39     Stream_write_char(stream, 'b');
40     Stream_write_char(stream, 'c');
41     assert(stream->length == 3);
42     assert(memcmp(stream->data, "abc", 3) == 0);
43     Stream_reset(stream);
44     Stream_write_char(stream, 'x');
45     Stream_write_char(stream, 'y');
46     assert(stream->length == 2);
47     assert(memcmp(stream->data, "xy", 2) == 0);
48     Stream_delete(stream);
49    
50     /* test writing file to stream */
51 siliconforks 93 stream = Stream_new(0);
52 siliconforks 230 FILE * f = xfopen("Makefile", "r");
53     fseek(f, 0, SEEK_END);
54     long file_length = ftell(f);
55     fseek(f, 0, SEEK_SET);
56     uint8_t * file_contents = xmalloc(file_length);
57     fread(file_contents, 1, file_length, f);
58     fseek(f, 0, SEEK_SET);
59     Stream_write_file_contents(stream, f);
60     fclose(f);
61 siliconforks 237 assert(stream->length == (size_t) file_length);
62 siliconforks 230 assert(memcmp(stream->data, file_contents, file_length) == 0);
63     free(file_contents);
64     Stream_delete(stream);
65    
66     stream = Stream_new(0);
67 siliconforks 93 Stream_printf(stream, "%s %d\n", "abc", 123);
68     assert(stream->length == 8);
69     assert(memcmp(stream->data, "abc 123\n", 8) == 0);
70     Stream_delete(stream);
71    
72 siliconforks 112 stream = Stream_new(10);
73     size_t length = 0;
74     for (int i = 0; i < 100; i++) {
75     Stream_printf(stream, "%s %d\n", "abc", i);
76     if (i < 10) {
77     length += 6;
78     }
79     else {
80     length += 7;
81     }
82     }
83     assert(stream->length == length);
84     length = 0;
85     for (int i = 0; i < 100; i++) {
86     char buffer[8];
87     int result = sprintf(buffer, "%s %d\n", "abc", i);
88     assert(memcmp(stream->data + length, buffer, result) == 0);
89     length += result;
90     }
91     assert(stream->length == length);
92     Stream_delete(stream);
93    
94     stream = Stream_new(10);
95     char buffer[100];
96     for (int i = 0; i < 100; i++) {
97     buffer[i] = 'x';
98     }
99     Stream_write(stream, buffer, 100);
100     assert(stream->length == 100);
101     for (int i = 0; i < 100; i++) {
102     assert(stream->data[i] == 'x');
103     }
104     Stream_delete(stream);
105    
106 siliconforks 93 exit(EXIT_SUCCESS);
107     }

  ViewVC Help
Powered by ViewVC 1.1.24