aboutsummaryrefslogtreecommitdiff
path: root/src/unit/hash.unit
diff options
context:
space:
mode:
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}