aboutsummaryrefslogtreecommitdiff
path: root/src/unit/hash.unit
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-01-07 15:59:57 +0000
committerMike Buland <eichlan@xagasoft.com>2009-01-07 15:59:57 +0000
commit45e065bc4fc93731ea9a0543462bc7cf9e6084d7 (patch)
treea9e8279fe00b9b01dc2393f59dc7f41b5e416b2a /src/unit/hash.unit
parentd96fe229e79f9b1947cbd24ff52d6bf7bb9bf80d (diff)
downloadlibbu++-45e065bc4fc93731ea9a0543462bc7cf9e6084d7.tar.gz
libbu++-45e065bc4fc93731ea9a0543462bc7cf9e6084d7.tar.bz2
libbu++-45e065bc4fc93731ea9a0543462bc7cf9e6084d7.tar.xz
libbu++-45e065bc4fc93731ea9a0543462bc7cf9e6084d7.zip
Only two real changes. First, Bu::FString and Bu::FBasicString are in different
files. This won't affect any programs at all anywhere. This will just make it easier to maintain and extend later. You still want to include "bu/fstring.h" and use Bu::FString in code. The other is kinda fun. I created a special format for unit tests, they use the extension .unit now and use the mkunit.sh script to convert them to c++ code. There are some nice features here too, maintaining unit tests is much, much easier, and we can have more features without making the code any harder to use. Also, it will be easier to have the unit tests generate reports and be run from a master program and the like.
Diffstat (limited to 'src/unit/hash.unit')
-rw-r--r--src/unit/hash.unit86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/unit/hash.unit b/src/unit/hash.unit
new file mode 100644
index 0000000..bd7da61
--- /dev/null
+++ b/src/unit/hash.unit
@@ -0,0 +1,86 @@
1// vim: syntax=cpp
2/*
3 * Copyright (C) 2007-2008 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 "bu/fstring.h"
10#include "bu/hash.h"
11
12#include <stdio.h>
13
14typedef Bu::Hash<Bu::FString, int> StrIntHash;
15typedef Bu::Hash<Bu::FString, Bu::FString> StrStrHash;
16typedef Bu::Hash<int, Bu::FString> IntStrHash;
17
18{=Init}
19
20{%probe1}
21{
22 StrIntHash h;
23 char buf[20];
24 for(int i=1;i<10000;i++)
25 {
26 sprintf(buf,"%d",i);
27 Bu::FString sTmp(buf);
28 h[sTmp] = i;
29 unitTest( h.has(sTmp) );
30 }
31}
32
33{%insert1}
34{
35 StrIntHash h;
36 h["Hi"] = 42;
37 unitTest( h["Hi"] == 42 );
38}
39
40{%insert2}
41{
42 StrStrHash h;
43 h["Hi"] = "Yo";
44 h["Bye"] = "Later";
45 unitTest( h["Hi"].getValue() == "Yo" );
46
47 StrStrHash h2(h);
48 unitTest( h2["Hi"].getValue() = "Yo" );
49 unitTest( h2["Bye"].getValue() = "Later" );
50
51 StrStrHash h3;
52 h3 = h;
53 unitTest( h3["Hi"].getValue() = "Yo" );
54 unitTest( h3["Bye"].getValue() = "Later" );
55}
56
57{%insert3}
58{
59 IntStrHash h;
60
61 for( unsigned int i=1; i<50; i++ )
62 {
63 h[i] = "testing";
64 unitTest( h.getSize() == i );
65 }
66}
67
68{%erase1}
69{
70 StrIntHash h;
71 h.insert("Number 1", 1 );
72 h.insert("Number 2", 2 );
73 h.insert("Number 3", 3 );
74 h.erase("Number 2");
75 h.get("Number 3");
76 try {
77 h.get("Number 2");
78 unitFailed("h.get(\"Number 2\") should have thrown an exception.");
79 } catch( Bu::HashException &e ) { }
80
81/* printf("\n");
82 for( StrIntHash::iterator i = h.begin(); i != h.end(); i++ )
83 {
84 printf(" - \"%s\" = %d\n", i.getKey().getStr(), i.getValue() );
85 } */
86}