aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_streams.cpp
blob: cbcbcacc5c06b6c6c1724437674c73aed7286831 (plain)
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
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "../shared/lua_api/stream.hpp"

int main(){
	//Simple
	struct stream* s = stream_create();
	stream_writeInt(s,5);
	assert(stream_readInt(s) == 5);
	stream_free(s);

	//Little more complex
	s = stream_create();
	stream_writeInt(s,1);
	stream_writeInt(s,2);
	stream_writeInt(s,3);
	stream_writeInt(s,4);
	assert(stream_readInt(s) == 1);
	assert(stream_readInt(s) == 2);
	assert(stream_readInt(s) == 3);
	assert(stream_readInt(s) == 4);
	stream_free(s);

	//Make sure we're not leaking memory
	//Uncomment this section, and check htop or windows task manager
	/*
	time_t t;
	time(&t);
	srand((unsigned) t);
	int i,j;
	for(i = 0; i < 100000; i++){
		s = stream_create();
		for(j = 0; j < 10000; j++){
			stream_writeInt(s,rand());
		}
		stream_free(s);
	}
	*/

	//Read/write data
	s = stream_create();
	char* str = (char*)"One two three four!\n";
	int slen = strlen(str);
	stream_writeData(s,str,slen);
	char out[slen+1];
	stream_readData(s,slen,out);
	out[slen] = '\0';
	assert(strcmp(out,str) == 0);
	stream_free(s);

	//Read/write string
	s = stream_create();
	stream_writeString(s,str,slen);
	char* rstr = stream_readString(s);
	assert(strcmp(rstr,str) == 0);
	stream_free(s);

	//Make sure the string reads it's null character
	s = stream_create();
	stream_writeString(s,str,slen);
	stream_writeInt(s,5);
	stream_print(s);
	stream_readString(s);
	stream_print(s);
	int i = stream_readInt(s);
	assert(i == 5);
	stream_free(s);

	printf("OK!\n");
	return 0;
}