aboutsummaryrefslogtreecommitdiff
path: root/src/unit
diff options
context:
space:
mode:
Diffstat (limited to 'src/unit')
-rw-r--r--src/unit/entities/unit30
-rw-r--r--src/unit/file.cpp111
-rw-r--r--src/unit/fstring.cpp28
-rw-r--r--src/unit/hash.cpp40
-rw-r--r--src/unit/hashtable/hashtable.cpp107
-rw-r--r--src/unit/membuf.cpp37
-rw-r--r--src/unit/taf.cpp48
-rw-r--r--src/unit/xml/xml.cpp59
8 files changed, 294 insertions, 166 deletions
diff --git a/src/unit/entities/unit b/src/unit/entities/unit
new file mode 100644
index 0000000..28db45f
--- /dev/null
+++ b/src/unit/entities/unit
@@ -0,0 +1,30 @@
1<?xml version="1.1" ?>
2<entity desc="Unit test framework">
3 <param name="name" required="yes" desc="Name of the class"/>
4 <file
5 name="source"
6 filename="{=name:%tolower}.cpp"
7>#include "bu/unitsuite.h"
8
9class Unit : public Bu::UnitSuite
10{
11public:
12 Unit()
13 {
14 setName("{=name}");
15 addTest( Unit::test01 );
16 }
17
18 virtual ~Unit()
19 {
20 }
21
22 void test01()
23 {
24 unitTest( 0 == 5 );
25 }
26};
27
28int main( int argc, char *argv[] ){ return Unit().run( argc, argv ); }
29</file>
30</entity>
diff --git a/src/unit/file.cpp b/src/unit/file.cpp
new file mode 100644
index 0000000..1eaaf36
--- /dev/null
+++ b/src/unit/file.cpp
@@ -0,0 +1,111 @@
1#include "unitsuite.h"
2#include "file.h"
3#include "exceptions.h"
4
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <unistd.h>
8
9class Unit : public Bu::UnitSuite
10{
11public:
12 Unit()
13 {
14 setName("File");
15 addTest( Unit::writeFull );
16 addTest( Unit::readBlocks );
17 addTest( Unit::readError1 );
18 addTest( Unit::readError2 );
19 }
20
21 virtual ~Unit() { }
22
23 //
24 // Tests go here
25 //
26 void writeFull()
27 {
28 Bu::File sf("testfile1", "wb");
29 for( int c = 0; c < 256; c++ )
30 {
31 unsigned char ch = (unsigned char)c;
32 sf.write( &ch, 1 );
33 unitTest( sf.tell() == c+1 );
34 }
35 //unitTest( sf.canRead() == false );
36 //unitTest( sf.canWrite() == true );
37 //unitTest( sf.canSeek() == true );
38 sf.close();
39 struct stat sdat;
40 stat("testfile1", &sdat );
41 unitTest( sdat.st_size == 256 );
42 }
43
44 void readBlocks()
45 {
46 Bu::File sf("testfile1", "rb");
47 unsigned char buf[50];
48 size_t total = 0;
49 for(;;)
50 {
51 size_t s = sf.read( buf, 50 );
52 for( size_t c = 0; c < s; c++ )
53 {
54 unitTest( buf[c] == (unsigned char)(c+total) );
55 }
56 total += s;
57 if( s < 50 )
58 {
59 unitTest( total == 256 );
60 unitTest( sf.isEOS() == true );
61 break;
62 }
63 }
64 sf.close();
65 }
66
67 void readError1()
68 {
69 try
70 {
71 Bu::File sf("doesn'texist", "rb");
72 unitFailed("No exception thrown");
73 }
74 catch( Bu::FileException &e )
75 {
76 return;
77 }
78 }
79
80 void readError2()
81 {
82 Bu::File sf("testfile1", "rb");
83 char buf[256];
84 int r = sf.read( buf, 256 );
85 unitTest( r == 256 );
86 // You have to read past the end to set the EOS flag.
87 unitTest( sf.isEOS() == false );
88 try
89 {
90 if( sf.read( buf, 5 ) > 0 )
91 {
92 unitFailed("Non-zero read result");
93 }
94 else
95 {
96 sf.close();
97 }
98 }
99 catch( Bu::FileException &e )
100 {
101 sf.close();
102 return;
103 }
104 }
105};
106
107int main( int argc, char *argv[] )
108{
109 return Unit().run( argc, argv );
110}
111
diff --git a/src/unit/fstring.cpp b/src/unit/fstring.cpp
new file mode 100644
index 0000000..72755eb
--- /dev/null
+++ b/src/unit/fstring.cpp
@@ -0,0 +1,28 @@
1#include "fstring.h"
2#include "unitsuite.h"
3
4class Unit : public Bu::UnitSuite
5{
6public:
7 Unit()
8 {
9 setName("FString");
10 addTest( Unit::test1 );
11 }
12
13 virtual ~Unit()
14 {
15 }
16
17 void test1()
18 {
19 unitTest( 1 == 1 );
20 unitTest( 1 == 0 );
21 }
22};
23
24int main( int argc, char *argv[] )
25{
26 return Unit().run( argc, argv );
27}
28
diff --git a/src/unit/hash.cpp b/src/unit/hash.cpp
new file mode 100644
index 0000000..9ea933f
--- /dev/null
+++ b/src/unit/hash.cpp
@@ -0,0 +1,40 @@
1#include "bu/fstring.h"
2#include "bu/hash.h"
3#include "bu/unitsuite.h"
4
5#include <stdio.h>
6
7class Unit : public Bu::UnitSuite
8{
9private:
10 typedef Bu::Hash<Bu::FString, int> StrIntHash;
11public:
12 Unit()
13 {
14 setName("Hash");
15 addTest( Unit::test_probe );
16 }
17
18 virtual ~Unit()
19 {
20 }
21
22 void test_probe()
23 {
24 StrIntHash h;
25 char buf[20];
26 for(int i=1;i<10000;i++)
27 {
28 sprintf(buf,"%d",i);
29 Bu::FString sTmp(buf);
30 h[sTmp] = i;
31 unitTest( h.has(sTmp) );
32 }
33 }
34};
35
36int main( int argc, char *argv[] )
37{
38 return Unit().run( argc, argv );
39}
40
diff --git a/src/unit/hashtable/hashtable.cpp b/src/unit/hashtable/hashtable.cpp
deleted file mode 100644
index b2e1cf5..0000000
--- a/src/unit/hashtable/hashtable.cpp
+++ /dev/null
@@ -1,107 +0,0 @@
1#include <cstdlib>
2#include <cstring>
3#include <iostream>
4#include <cpptest.h>
5#include <string.h>
6#include <set>
7#include <map>
8
9#include "hashfunctionstring.h"
10#include "hashfunctioncasestring.h"
11#include "hashfunctionint.h"
12
13class HashFunctionSuite : public Test::Suite
14{
15public:
16 HashFunctionSuite()
17 {
18 TEST_ADD( HashFunctionSuite::functionString )
19 TEST_ADD( HashFunctionSuite::functionCaseString )
20 TEST_ADD( HashFunctionSuite::functionInt )
21 }
22
23private:
24 void functionStringWorker( HashFunction &hf, std::set<unsigned long> &sCodes, char *str, int nLevel, int nMax )
25 {
26 for( char let = 'A'; let <= 'z'; let += 3 )
27 {
28 str[nLevel+1] = '\0';
29 str[nLevel] = let;
30 unsigned long x = hf.hash( str );
31 TEST_ASSERT( sCodes.find( x ) == sCodes.end() );
32 TEST_ASSERT( hf.cmpIDs( str, str ) );
33 sCodes.insert( x );
34
35 if( nLevel < nMax )
36 functionStringWorker( hf, sCodes, str, nLevel+1, nMax );
37 }
38 }
39
40 void functionString()
41 {
42 HashFunctionString hf;
43 char str[10];
44
45 std::set<unsigned long> sCodes;
46
47 functionStringWorker( hf, sCodes, str, 0, 3 );
48 }
49
50 void functionCaseStringWorker( HashFunction &hf, std::map<unsigned long, char *> &sCodes, char *str, int nLevel, int nMax )
51 {
52 for( char let = 'A'; let <= 'z'; let += 3 )
53 {
54 str[nLevel+1] = '\0';
55 str[nLevel] = let;
56 unsigned long x = hf.hash( str );
57 std::map<unsigned long, char *>::iterator i = sCodes.find( x );
58 if( i == sCodes.end() )
59 {
60 sCodes[x] = strdup( str );
61 }
62 else
63 {
64 TEST_ASSERT( strcasecmp( (*i).second, str ) == 0 );
65 TEST_ASSERT( hf.cmpIDs( (*i).second, str ) == true );
66 }
67
68 if( nLevel < nMax )
69 functionCaseStringWorker( hf, sCodes, str, nLevel+1, nMax );
70 }
71 }
72
73 void functionCaseString()
74 {
75 HashFunctionCaseString hf;
76 char str[10];
77
78 std::map<unsigned long, char *> sCodes;
79
80 functionCaseStringWorker( hf, sCodes, str, 0, 3 );
81
82 std::map<unsigned long, char *>::iterator i;
83 for( i = sCodes.begin(); i != sCodes.end(); i++ )
84 {
85 free( (*i).second );
86 }
87 }
88
89 void functionInt()
90 {
91 HashFunctionInt hf;
92
93 for( long i = -100000; i <= 100000; i += 100 )
94 {
95 TEST_ASSERT( ((long)hf.hash( (void *)i )) == i );
96 TEST_ASSERT( ((long)hf.cmpIDs( (void *)i, (void *)i )) );
97 }
98 }
99};
100
101int main( int argc, char *argv[] )
102{
103 Test::TextOutput output( Test::TextOutput::Verbose );
104 HashFunctionSuite ts;
105 return ts.run( output ) ? EXIT_SUCCESS : EXIT_FAILURE;
106}
107
diff --git a/src/unit/membuf.cpp b/src/unit/membuf.cpp
new file mode 100644
index 0000000..65ba82a
--- /dev/null
+++ b/src/unit/membuf.cpp
@@ -0,0 +1,37 @@
1#include "bu/unitsuite.h"
2#include "bu/membuf.h"
3
4class Unit : public Bu::UnitSuite
5{
6public:
7 Unit()
8 {
9 setName("MemBuf");
10 addTest( Unit::testWriteRead01 );
11 }
12
13 virtual ~Unit()
14 {
15 }
16
17 void testWriteRead01()
18 {
19 Bu::MemBuf mb;
20 unitTest( mb.write("ab", 2 ) == 2 );
21 unitTest( mb.write("cde", 3 ) == 3 );
22 unitTest( mb.write("FG", 2 ) == 2 );
23
24 mb.setPos( 0 );
25
26 char buf[8];
27 buf[7] = '\0';
28 unitTest( mb.read( buf, 7 ) == 7 );
29 unitTest( !strncmp( buf, "abcdeFG", 7 ) );
30 unitTest( mb.read( buf, 7 ) == 0 );
31 mb.seek( -3 );
32 unitTest( mb.read( buf, 7 ) == 3 );
33 unitTest( !strncmp( buf, "eFG", 3 ) );
34 }
35};
36
37int main( int argc, char *argv[] ){ return Unit().run( argc, argv ); }
diff --git a/src/unit/taf.cpp b/src/unit/taf.cpp
new file mode 100644
index 0000000..5e0e914
--- /dev/null
+++ b/src/unit/taf.cpp
@@ -0,0 +1,48 @@
1#include "unitsuite.h"
2#include "file.h"
3#include "tafreader.h"
4
5#include <string.h>
6#include <unistd.h>
7
8class Unit : public Bu::UnitSuite
9{
10public:
11 Unit()
12 {
13 setName("taf");
14 addTest( Unit::read1 );
15 }
16
17 virtual ~Unit()
18 {
19 }
20
21 void read1()
22 {
23#define FN_TMP ("/tmp/tmpXXXXXXXX")
24 Bu::FString sFnTmp(FN_TMP);
25 Bu::File fOut = Bu::File::tempFile( sFnTmp, "wb" );
26 const char *data =
27"{test: name=\"Bob\"}"
28;
29 fOut.write(data,strlen(data));
30 fOut.close();
31
32 Bu::File fIn(sFnTmp.c_str(), "rb");
33 Bu::TafReader tr(fIn);
34
35 Bu::TafGroup *tn = tr.readGroup();
36 unitTest( !strcmp("Bob", tn->getProperty("name").c_str()) );
37 delete tn;
38
39 unlink(sFnTmp.c_str());
40#undef FN_TMP
41 }
42};
43
44int main( int argc, char *argv[] )
45{
46 return Unit().run( argc, argv );
47}
48
diff --git a/src/unit/xml/xml.cpp b/src/unit/xml/xml.cpp
deleted file mode 100644
index e4d779c..0000000
--- a/src/unit/xml/xml.cpp
+++ /dev/null
@@ -1,59 +0,0 @@
1#include <cstdlib>
2#include <cstring>
3#include <iostream>
4#include <cpptest.h>
5#include <string.h>
6
7#include "xmlstringreader.h"
8#include "xmlexception.h"
9
10class XmlCoreTestSuite : public Test::Suite
11{
12public:
13 XmlCoreTestSuite()
14 {
15 TEST_ADD( XmlCoreTestSuite::badXml01 )
16 TEST_ADD( XmlCoreTestSuite::badXml02 )
17 TEST_ADD( XmlCoreTestSuite::badXml03 )
18
19 TEST_ADD( XmlCoreTestSuite::entityBuiltin01 )
20
21 TEST_ADD( XmlCoreTestSuite::entityDoc01 )
22 }
23
24private:
25 void badXml01()
26 {
27 TEST_THROWS( XmlStringReader r("<hello></bye>"), XmlException & );
28 }
29
30 void badXml02()
31 {
32 TEST_THROWS( XmlStringReader r("<hello>"), XmlException & );
33 }
34
35 void badXml03()
36 {
37 TEST_THROWS( XmlStringReader r("<hello param=\"stuff?"), XmlException & );
38 }
39
40 void entityBuiltin01()
41 {
42 XmlStringReader r("<hello>&gt;&lt;&amp;&apos;&quot;</hello>");
43 TEST_ASSERT( strcmp( r.getRoot()->getContent(), "><&\'\"" ) == 0 );
44 }
45
46 void entityDoc01()
47 {
48 XmlStringReader r("<!ENTITY name \"bob the man\"><hello>&quot;&name;&quot;</hello>");
49 TEST_ASSERT( strcmp( r.getRoot()->getContent(), "\"bob the man\"" ) == 0 );
50 }
51};
52
53int main( int argc, char *argv[] )
54{
55 Test::TextOutput output( Test::TextOutput::Verbose );
56 XmlCoreTestSuite ts;
57 return ts.run( output ) ? EXIT_SUCCESS : EXIT_FAILURE;
58}
59