PageRenderTime 93ms CodeModel.GetById 61ms app.highlight 9ms RepoModel.GetById 11ms app.codeStats 10ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/tests/streams.c

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 107 lines | 77 code | 10 blank | 20 comment | 21 complexity | 8526ea28e4bb72322fb020170e5e50b5 MD5 | raw file
  1/*
  2    streams.c - test `Stream' object
  3    Copyright (C) 2008 siliconforks.com
  4
  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#include "util.h"
 25
 26int 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  /* 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  stream = Stream_new(0);
 52  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  assert(stream->length == (size_t) file_length);
 62  assert(memcmp(stream->data, file_contents, file_length) == 0);
 63  free(file_contents);
 64  Stream_delete(stream);
 65
 66  stream = Stream_new(0);
 67  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  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  exit(EXIT_SUCCESS);
107}