diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/unit/queuebuf.unit | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/unit/queuebuf.unit b/src/unit/queuebuf.unit new file mode 100644 index 0000000..9ae7d36 --- /dev/null +++ b/src/unit/queuebuf.unit | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | // vim: syntax=cpp | ||
| 2 | /* | ||
| 3 | * Copyright (C) 2007-2010 Xagasoft, All rights reserved. | ||
| 4 | * | ||
| 5 | * This file is part of the libbu++ library and is released under the | ||
| 6 | * terms of the license contained in the file LICENSE. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <stdlib.h> | ||
| 10 | #include <time.h> | ||
| 11 | |||
| 12 | #include "bu/queuebuf.h" | ||
| 13 | #include "bu/md5.h" | ||
| 14 | |||
| 15 | #define RNDCHR ((char)(((double)random()/(double)RAND_MAX)*256.0)) | ||
| 16 | |||
| 17 | {=Init} | ||
| 18 | |||
| 19 | {%testBasic01} | ||
| 20 | { | ||
| 21 | Bu::QueueBuf qb; | ||
| 22 | unitTest( qb.write("ab", 2 ) == 2 ); | ||
| 23 | unitTest( qb.write("cde", 3 ) == 3 ); | ||
| 24 | unitTest( qb.write("FG", 2 ) == 2 ); | ||
| 25 | |||
| 26 | char buf[8]; | ||
| 27 | buf[7] = '\0'; | ||
| 28 | unitTest( qb.read( buf, 7 ) == 7 ); | ||
| 29 | unitTest( !strncmp( buf, "abcdeFG", 7 ) ); | ||
| 30 | unitTest( qb.read( buf, 7 ) == 0 ); | ||
| 31 | } | ||
| 32 | |||
| 33 | void QBUF_RANDSTR( Bu::FString &fill, unsigned int iSize ) | ||
| 34 | { | ||
| 35 | char c; | ||
| 36 | for( unsigned int i=0; i<iSize; ++i ) | ||
| 37 | { | ||
| 38 | c = RNDCHR; | ||
| 39 | fill.append(&c,1); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | {%testAmounts} | ||
| 44 | { | ||
| 45 | srandom(time(NULL)); | ||
| 46 | Bu::QueueBuf qb; | ||
| 47 | Bu::FString sTmp; | ||
| 48 | char buf[4096]; | ||
| 49 | |||
| 50 | for( int i=0; i<200; ++i ) | ||
| 51 | { | ||
| 52 | unsigned int iAmt = (int)RNDCHR+128; | ||
| 53 | sTmp.clear(); | ||
| 54 | QBUF_RANDSTR( sTmp, iAmt ); | ||
| 55 | unitTest( qb.write( sTmp.getStr(), sTmp.getSize() ) == | ||
| 56 | (uint32_t)sTmp.getSize() ); | ||
| 57 | size_t iRead = qb.read( buf, 4096 ); | ||
| 58 | unitTest( iRead == iAmt ); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | void QBUF_HEXOUT( const char *s, int iSize ) | ||
| 63 | { | ||
| 64 | for( int i=0; i<iSize; ++i ) | ||
| 65 | printf("%02x",(int)(uint8_t)s[i]); | ||
| 66 | } | ||
| 67 | |||
| 68 | void QBUF_HASH( Bu::FString &fill, const char *s, int iSize ) | ||
| 69 | { | ||
| 70 | Bu::Md5 hash; | ||
| 71 | hash.reset(); | ||
| 72 | hash.addData( s, iSize ); | ||
| 73 | const Bu::FString &sTmp = hash.getResult(); | ||
| 74 | fill.append( sTmp.getStr(), 16 ); | ||
| 75 | } | ||
| 76 | |||
| 77 | {%testRandomData} | ||
| 78 | { | ||
| 79 | srandom(time(NULL)); | ||
| 80 | Bu::QueueBuf qb; | ||
| 81 | Bu::FString sTmp; | ||
| 82 | Bu::FString sTmp2; | ||
| 83 | char buf[4096]; | ||
| 84 | |||
| 85 | for( int i=0; i<200; ++i ) | ||
| 86 | { | ||
| 87 | uint32_t iAmt = (uint32_t)RNDCHR+128; | ||
| 88 | sTmp.clear(); | ||
| 89 | sTmp.append( (const char *)&iAmt, 4 ); | ||
| 90 | QBUF_RANDSTR( sTmp, iAmt ); | ||
| 91 | sTmp2.clear(); | ||
| 92 | QBUF_HASH( sTmp2, sTmp.getStr()+4, iAmt ); | ||
| 93 | sTmp.append( sTmp2 ); | ||
| 94 | unitTest( qb.write( sTmp.getStr(), sTmp.getSize() ) == | ||
| 95 | (uint32_t)sTmp.getSize() ); | ||
| 96 | size_t iRead = qb.read( buf, 4096 ); | ||
| 97 | uint32_t iGotSize = *((uint32_t *)buf); | ||
| 98 | unitTest( iRead == iGotSize+4+16 ); | ||
| 99 | sTmp2.clear(); | ||
| 100 | QBUF_HASH( sTmp2, buf+4, iGotSize ); | ||
| 101 | unitTest( !strncmp(sTmp2.getStr(),buf+4+iGotSize,16) ); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
