diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-07-03 00:28:59 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-07-03 00:28:59 +0000 |
commit | ac517a2b7625e0aa0862679e961c6349f859ea3b (patch) | |
tree | e3e27a6b9bd5e2be6150088495c91fc91786ad9d /src/unit | |
parent | f8d4301e9fa4f3709258505941e37fab2eadadc6 (diff) | |
parent | bd865cee5f89116c1f054cd0e5c275e97c2d0a9b (diff) | |
download | libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.gz libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.bz2 libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.xz libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.zip |
The reorg is being put in trunk, I think it's ready. Now we just get to find
out how many applications won't work anymore :)
Diffstat (limited to '')
-rw-r--r-- | src/old/unit/hashtable/hashtable.cpp (renamed from src/unit/hashtable/hashtable.cpp) | 0 | ||||
-rw-r--r-- | src/old/unit/xml/xml.cpp (renamed from src/unit/xml/xml.cpp) | 0 | ||||
-rw-r--r-- | src/unit/entities/unit | 30 | ||||
-rw-r--r-- | src/unit/file.cpp | 111 | ||||
-rw-r--r-- | src/unit/fstring.cpp | 28 | ||||
-rw-r--r-- | src/unit/hash.cpp | 40 | ||||
-rw-r--r-- | src/unit/membuf.cpp | 37 | ||||
-rw-r--r-- | src/unit/taf.cpp | 48 | ||||
-rw-r--r-- | src/unitsuite.cpp | 67 | ||||
-rw-r--r-- | src/unitsuite.h | 96 |
10 files changed, 457 insertions, 0 deletions
diff --git a/src/unit/hashtable/hashtable.cpp b/src/old/unit/hashtable/hashtable.cpp index b2e1cf5..b2e1cf5 100644 --- a/src/unit/hashtable/hashtable.cpp +++ b/src/old/unit/hashtable/hashtable.cpp | |||
diff --git a/src/unit/xml/xml.cpp b/src/old/unit/xml/xml.cpp index e4d779c..e4d779c 100644 --- a/src/unit/xml/xml.cpp +++ b/src/old/unit/xml/xml.cpp | |||
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 | |||
9 | class Unit : public Bu::UnitSuite | ||
10 | { | ||
11 | public: | ||
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 | |||
28 | int 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 | |||
9 | class Unit : public Bu::UnitSuite | ||
10 | { | ||
11 | public: | ||
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 | |||
107 | int 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 | |||
4 | class Unit : public Bu::UnitSuite | ||
5 | { | ||
6 | public: | ||
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 | |||
24 | int 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 | |||
7 | class Unit : public Bu::UnitSuite | ||
8 | { | ||
9 | private: | ||
10 | typedef Bu::Hash<Bu::FString, int> StrIntHash; | ||
11 | public: | ||
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 | |||
36 | int main( int argc, char *argv[] ) | ||
37 | { | ||
38 | return Unit().run( argc, argv ); | ||
39 | } | ||
40 | |||
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 | |||
4 | class Unit : public Bu::UnitSuite | ||
5 | { | ||
6 | public: | ||
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 | |||
37 | int 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 | |||
8 | class Unit : public Bu::UnitSuite | ||
9 | { | ||
10 | public: | ||
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 | |||
44 | int main( int argc, char *argv[] ) | ||
45 | { | ||
46 | return Unit().run( argc, argv ); | ||
47 | } | ||
48 | |||
diff --git a/src/unitsuite.cpp b/src/unitsuite.cpp new file mode 100644 index 0000000..2a28eb5 --- /dev/null +++ b/src/unitsuite.cpp | |||
@@ -0,0 +1,67 @@ | |||
1 | #include "unitsuite.h" | ||
2 | |||
3 | Bu::UnitSuite::UnitSuite() | ||
4 | { | ||
5 | } | ||
6 | |||
7 | Bu::UnitSuite::~UnitSuite() | ||
8 | { | ||
9 | } | ||
10 | |||
11 | int Bu::UnitSuite::run( int argc, char *argv[] ) | ||
12 | { | ||
13 | for( TestList::iterator i = lTests.begin(); i != lTests.end(); i++ ) | ||
14 | { | ||
15 | printf("%s: ", i->sName.getStr() ); | ||
16 | fflush( stdout ); | ||
17 | try | ||
18 | { | ||
19 | (this->*(i->fTest))(); | ||
20 | printf("passed.\n"); | ||
21 | } | ||
22 | catch( Failed &e ) | ||
23 | { | ||
24 | if( e.bFile ) | ||
25 | { | ||
26 | printf("unitTest(%s) failed. (%s:%d)\n", | ||
27 | e.str.getStr(), | ||
28 | e.sFile.getStr(), | ||
29 | e.nLine | ||
30 | ); | ||
31 | } | ||
32 | else | ||
33 | { | ||
34 | printf("unitTest(%s) failed.\n", | ||
35 | e.str.getStr() | ||
36 | ); | ||
37 | } | ||
38 | } | ||
39 | catch( ... ) | ||
40 | { | ||
41 | printf("failed with external exception.\n"); | ||
42 | } | ||
43 | } | ||
44 | |||
45 | return 0; | ||
46 | } | ||
47 | |||
48 | void Bu::UnitSuite::add( Test fTest, Bu::FString sName ) | ||
49 | { | ||
50 | TestInfo ti; | ||
51 | ti.sName = sName; | ||
52 | long index = ti.sName.rfind("::"); | ||
53 | if( index != -1 ) | ||
54 | { | ||
55 | FString tmp = sSuiteName; | ||
56 | tmp += ti.sName.getStr()+index; | ||
57 | ti.sName = tmp; | ||
58 | } | ||
59 | ti.fTest = fTest; | ||
60 | lTests.push_back( ti ); | ||
61 | } | ||
62 | |||
63 | void Bu::UnitSuite::setName( const FString &sName ) | ||
64 | { | ||
65 | sSuiteName = sName; | ||
66 | } | ||
67 | |||
diff --git a/src/unitsuite.h b/src/unitsuite.h new file mode 100644 index 0000000..578b4cc --- /dev/null +++ b/src/unitsuite.h | |||
@@ -0,0 +1,96 @@ | |||
1 | #ifndef BU_UNIT_SUITE_H | ||
2 | #define BU_UNIT_SUITE_H | ||
3 | |||
4 | #include <stdint.h> | ||
5 | #include <list> | ||
6 | #include "fstring.h" | ||
7 | |||
8 | namespace Bu | ||
9 | { | ||
10 | /** | ||
11 | * Provides a unit testing framework. This is pretty easy to use, probably | ||
12 | * the best way to get started is to use ch to generate a template, or use | ||
13 | * the code below with appropriate tweaks: | ||
14 | *@code | ||
15 | * #include "unitsuite.h" | ||
16 | * | ||
17 | * class Unit : public Bu::UnitSuite | ||
18 | * { | ||
19 | * public: | ||
20 | * Unit() | ||
21 | * { | ||
22 | * setName("Example"); | ||
23 | * addTest( Unit::test ); | ||
24 | * } | ||
25 | * | ||
26 | * virtual ~Unit() { } | ||
27 | * | ||
28 | * // | ||
29 | * // Tests go here | ||
30 | * // | ||
31 | * void test() | ||
32 | * { | ||
33 | * unitTest( 1 == 1 ); | ||
34 | * } | ||
35 | * }; | ||
36 | * | ||
37 | * int main( int argc, char *argv[] ) | ||
38 | * { | ||
39 | * return Unit().run( argc, argv ); | ||
40 | * } | ||
41 | * | ||
42 | @endcode | ||
43 | * The main function can contain other things, but using this one exactly | ||
44 | * makes all of the test suites work exactly the same. Using the optional | ||
45 | * setName at the top of the constructor replaces the class name with the | ||
46 | * chosen name when printing out stats and info. | ||
47 | */ | ||
48 | class UnitSuite | ||
49 | { | ||
50 | public: | ||
51 | UnitSuite(); | ||
52 | virtual ~UnitSuite(); | ||
53 | |||
54 | int run( int argc=0, char *argv[]=NULL ); | ||
55 | |||
56 | typedef void (UnitSuite::*Test)(); | ||
57 | |||
58 | class Failed | ||
59 | { | ||
60 | public: | ||
61 | Failed() : str(""), bFile( false ) { } | ||
62 | Failed( const FString &s ) : str( s ), bFile( false ) { } | ||
63 | Failed( const FString &s, const FString &sFile, int nLine ) : | ||
64 | str( s ), sFile( sFile ), nLine( nLine ), bFile( true ) { } | ||
65 | |||
66 | FString str; | ||
67 | FString sFile; | ||
68 | int nLine; | ||
69 | bool bFile; | ||
70 | }; | ||
71 | |||
72 | protected: | ||
73 | void add( Test fTest, Bu::FString sName ); | ||
74 | void setName( const FString &sName ); | ||
75 | |||
76 | private: | ||
77 | typedef struct TestInfo | ||
78 | { | ||
79 | FString sName; | ||
80 | Test fTest; | ||
81 | } TestInfo; | ||
82 | |||
83 | typedef std::list<TestInfo> TestList; | ||
84 | TestList lTests; | ||
85 | FString sSuiteName; | ||
86 | }; | ||
87 | } | ||
88 | |||
89 | #define addTest( fn ) add( static_cast<Bu::UnitSuite::Test>(&fn), #fn ) | ||
90 | #define unitTest( tst ) if( !(tst) ) \ | ||
91 | { \ | ||
92 | throw Bu::UnitSuite::Failed( #tst, __FILE__, __LINE__ ); \ | ||
93 | } | ||
94 | #define unitFailed( msg ) throw Bu::UnitSuite::Failed(msg, __FILE__, __LINE__); | ||
95 | |||
96 | #endif | ||