PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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