aboutsummaryrefslogtreecommitdiff
path: root/src/unit/queuebuf.unit
blob: 9ae7d36cbb5c7ace297e5577aa19dbf625cbc2ef (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
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
// vim: syntax=cpp
/*
 * Copyright (C) 2007-2010 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include <stdlib.h>
#include <time.h>

#include "bu/queuebuf.h"
#include "bu/md5.h"

#define RNDCHR ((char)(((double)random()/(double)RAND_MAX)*256.0))

{=Init}

{%testBasic01}
{
	Bu::QueueBuf qb;
	unitTest( qb.write("ab", 2 ) == 2 );
	unitTest( qb.write("cde", 3 ) == 3 );
	unitTest( qb.write("FG", 2 ) == 2 );

	char buf[8];
	buf[7] = '\0';
	unitTest( qb.read( buf, 7 ) == 7 );
	unitTest( !strncmp( buf, "abcdeFG", 7 ) );
	unitTest( qb.read( buf, 7 ) == 0 );
}

void QBUF_RANDSTR( Bu::FString &fill, unsigned int iSize )
{
	char c;
	for( unsigned int i=0; i<iSize; ++i )
	{
		c = RNDCHR;
		fill.append(&c,1);
	}
}

{%testAmounts}
{
	srandom(time(NULL));
	Bu::QueueBuf qb;
	Bu::FString sTmp;
	char buf[4096];

	for( int i=0; i<200; ++i )
	{
		unsigned int iAmt = (int)RNDCHR+128;
		sTmp.clear();
		QBUF_RANDSTR( sTmp, iAmt );
		unitTest( qb.write( sTmp.getStr(), sTmp.getSize() ) == 
			(uint32_t)sTmp.getSize() );
		size_t iRead = qb.read( buf, 4096 );
		unitTest( iRead == iAmt );
	}
}

void QBUF_HEXOUT( const char *s, int iSize )
{
	for( int i=0; i<iSize; ++i )
		printf("%02x",(int)(uint8_t)s[i]);
}

void QBUF_HASH( Bu::FString &fill, const char *s, int iSize )
{
	Bu::Md5 hash;
	hash.reset();
	hash.addData( s, iSize );
	const Bu::FString &sTmp = hash.getResult();
	fill.append( sTmp.getStr(), 16 );
}

{%testRandomData}
{
	srandom(time(NULL));
	Bu::QueueBuf qb;
	Bu::FString sTmp;
	Bu::FString sTmp2;
	char buf[4096];

	for( int i=0; i<200; ++i )
	{
		uint32_t iAmt = (uint32_t)RNDCHR+128;
		sTmp.clear();
		sTmp.append( (const char *)&iAmt, 4 );
		QBUF_RANDSTR( sTmp, iAmt );
		sTmp2.clear();
		QBUF_HASH( sTmp2, sTmp.getStr()+4, iAmt );
		sTmp.append( sTmp2 );
		unitTest( qb.write( sTmp.getStr(), sTmp.getSize() ) == 
			(uint32_t)sTmp.getSize() );
		size_t iRead = qb.read( buf, 4096 );
		uint32_t iGotSize = *((uint32_t *)buf);
		unitTest( iRead == iGotSize+4+16 );
		sTmp2.clear();
		QBUF_HASH( sTmp2, buf+4, iGotSize );
		unitTest( !strncmp(sTmp2.getStr(),buf+4+iGotSize,16) );
	}
}