summaryrefslogtreecommitdiff
path: root/src/unit/queuebuf.unit
blob: 553d9fcd5bdce1f1e510f9b7248638c325155356 (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-2012 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))

suite QueueBuf
{
	test 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::String &fill, unsigned int iSize )
	{
		char c;
		for( unsigned int i=0; i<iSize; ++i )
		{
			c = RNDCHR;
			fill.append(&c,1);
		}
	}

	test testAmounts
	{
		srandom(time(NULL));
		Bu::QueueBuf qb;
		Bu::String 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::String &fill, const char *s, int iSize )
	{
		Bu::Md5 hash;
		hash.reset();
		hash.addData( s, iSize );
		const Bu::String &sTmp = hash.getResult();
		fill.append( sTmp.getStr(), 16 );
	}

	test testRandomData
	{
		srandom(time(NULL));
		Bu::QueueBuf qb;
		Bu::String sTmp;
		Bu::String 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) );
		}
	}
}