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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include "Config.h"
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <time.h>
#include "Bootstrap.h"
#include "OutputStream.h"
#include "File.h"
#include "Str.h"
/**
* OutputStream.c unit tests.
*/
#define STDOUT 1
#define TIMEOUT 50
int main(void) {
OutputStream_T out = NULL;
Bootstrap(); // Need to initialize library
printf("============> Start OutputStream Tests\n\n");
printf("=> Test0: create/destroy the file input stream\n");
{
out = OutputStream_new(STDOUT);
assert(!OutputStream_isClosed(out));
OutputStream_free(&out);
assert(out == NULL);
}
printf("=> Test0: OK\n\n");
printf("=> Test1: get/set timeout\n");
{
out = OutputStream_new(STDOUT);
printf("\tCurrent timeout: %ldms\n", OutputStream_getTimeout(out));
OutputStream_setTimeout(out, TIMEOUT);
assert(OutputStream_getTimeout(out) == TIMEOUT);
printf("\tTimeout set to: %dms\n", TIMEOUT);
OutputStream_free(&out);
}
printf("=> Test1: OK\n\n");
printf("=> Test2: write buffer bytes, test flush and status\n");
{
int bytes = strlen("line1\nline2\nline3\n");
char data[] = "line1\nline2\nline3\n";
out = OutputStream_new(STDOUT);
assert(0 == OutputStream_buffered(out));
assert(bytes == OutputStream_write(out, data, (int)strlen(data)));
assert(bytes == OutputStream_buffered(out));
assert(0 == OutputStream_getBytesWritten(out));
OutputStream_flush(out);
assert(bytes == OutputStream_getBytesWritten(out));
// Test writing lower bytes
OutputStream_clear(out);
char b[] = {0,0,0,0};
assert(OutputStream_write(out, b, 4) == 4);
OutputStream_free(&out);
}
printf("=> Test2: OK\n\n");
printf("=> Test3: printf format\n");
{
out = OutputStream_new(STDOUT);
// Test supported format specifiers
assert(OutputStream_print(out, "%s", "hello") == 5);
assert(OutputStream_print(out, "%c", 'h') == 1);
assert(OutputStream_print(out, "%d", 12345) == 5);
assert(OutputStream_print(out, "%i", -12345) == 6);
assert(OutputStream_print(out, "%u", 12345) == 5);
assert(OutputStream_print(out, "%o", 8) == 2); //10
assert(OutputStream_print(out, "%x", 255) == 2); //ff
assert(OutputStream_print(out, "%e", 12.34) == 12); //1.234000e+01
assert(OutputStream_print(out, "%f", -12.34) == 10); //-12.340000
assert(OutputStream_print(out, "%g", 12.34) == 6); //012.34
OutputStream_print(out, "%p", out);
assert(OutputStream_print(out, "%%hello%%") == 7);
// Length modifier
assert(OutputStream_print(out, "%ld", 32767L) == 5);
assert(OutputStream_print(out, "%li", -32767L) == 6);
assert(OutputStream_print(out, "%lu", 32767L) == 5);
assert(OutputStream_print(out, "%lo", 32767L) == 5); //77777
assert(OutputStream_print(out, "%lx", 32767L) == 4); //7fff
// Test width and precision
assert(OutputStream_print(out, "%16.16s\n%16.16s\n", "hello", "world") == 34);
assert(OutputStream_print(out, "%-16.16s\n%-16.16s\n", "hello", "world") == 34);
assert(OutputStream_print(out, "%.2f", 12.3456789) == 5); //12.35
assert(OutputStream_print(out, "%02d", 3) == 2); // 03
OutputStream_clear(out);
OutputStream_free(&out);
}
printf("=> Test3: OK\n\n");
printf("=> Test4: write two concatenated lines\n");
{
int i = 0;
out = OutputStream_new(STDOUT);
while (i++ < 2) {
OutputStream_print(out, "line%d", i);
}
OutputStream_print(out, "\n");
OutputStream_flush(out);
OutputStream_free(&out);
}
printf("=> Test4: OK\n\n");
printf("=> Test5: output reset - just the line[5-9] should show\n");
{
int i = 0;
out = OutputStream_new(STDOUT);
while (i++ < 9) {
OutputStream_print(out, "line%d\n", i);
if (i == 4) {
OutputStream_clear(out);
}
}
OutputStream_flush(out);
OutputStream_free(&out);
}
printf("=> Test5: OK\n\n");
printf("=> Test6: wrong descriptor - expecting write fail\n");
{
out = OutputStream_new(999);
TRY
OutputStream_print(out, "Should not show");
assert(OutputStream_flush(out) != -1);
assert(false); // Should not come here
CATCH(AssertException)
// Passed
END_TRY;
OutputStream_free(&out);
}
printf("=> Test6: OK\n\n");
printf("=> Test7: printf large buffer\n");
{
// Note, test assume OutputStream buffer is 1500
char a[1024];
memset(a, 'x', 1024);
a[1023]= 0;
out = OutputStream_new(STDOUT);
// Test fit into buffer
assert(OutputStream_print(out, "data: %s\n", a) == 1030);
assert(OutputStream_flush(out) == 1030);
// Test when data is larger than OutputStream buffer
char b[2048];
memset(b, 'y', 2048);
b[2047] = 0;
assert(OutputStream_print(out, "data: %s\n", b) == 2054);
OutputStream_free(&out);
}
printf("=> Test7: OK\n\n");
printf("============> OutputStream Tests: OK\n\n");
return 0;
}
|