LRAUV  revA
FastMap_Test.h
Go to the documentation of this file.
1 
9 #ifndef _FASTMAP_TEST_H_
10 #define _FASTMAP_TEST_H_
11 
12 #include "utils/FastMap.h"
13 #include "utils/Str.h"
14 
15 #include <cxxtest/TestSuite.h>
16 #include <stdlib.h>
17 
23 class FastMap_Test : public CxxTest::TestSuite
24 {
25 public:
26 
29  {
31 
32  Str key1( "Key1" );
33  Str key2( "Key2" );
34  Str key3( "Key3" );
35  Str key4( "Key4" );
36  Str key5( "Key5" );
37 
38  const Str entry1( "Entry1" );
39  const Str entry2( "Entry2" );
40  const Str entry3( "Entry3" );
41  const Str entry4( "Entry4" );
42  const Str entry4a( "Entry4a" );
43 
44  TS_ASSERT_EQUALS( map.size(), 0U );
45 
46  map.put( key2, &entry2 );
47  TS_ASSERT_EQUALS( map.size(), 1U );
48 
49  map.put( key4, &entry4 );
50  TS_ASSERT_EQUALS( map.size(), 2U );
51 
52  map.put( key1, &entry1 );
53  TS_ASSERT_EQUALS( map.size(), 3U );
54 
55  map.put( key3, &entry3 );
56  TS_ASSERT_EQUALS( map.size(), 4U );
57 
58  // Repeat entry of key 4
59  map.put( key4, &entry4a );
60  TS_ASSERT_EQUALS( map.size(), 4U );
61 
62  TS_ASSERT_EQUALS( *map.get( key1 ), entry1 );
63  TS_ASSERT_EQUALS( *map.get( key2 ), entry2 );
64  TS_ASSERT_EQUALS( *map.get( key3 ), entry3 );
65  TS_ASSERT_EQUALS( *map.get( key4 ), entry4 );
66  TS_ASSERT_EQUALS( map.get( key5 ), ( void* )0 );
67 
68  }
69 
72  {
74 
75  Str key21( "Key21" );
76  Str key22( "Key22" );
77  Str key23( "Key23" );
78  Str key24( "Key24" );
79  Str key25( "Key25" );
80 
81  map2.put( key22, new Str( "Entry2.2" ) );
82  map2.put( key24, new Str( "Entry2.4" ) );
83  map2.put( key21, new Str( "Entry2.1" ) );
84  map2.put( key23, new Str( "Entry2.3" ) );
85  Str* str = new Str( "Entry2.4a" );
86  if( !map2.put( key24, str ) )
87  {
88  delete str;
89  }
90 
91  TS_ASSERT_SAME_DATA( map2.get( key21 )->cStr(), "Entry2.1", 9 );
92  TS_ASSERT_SAME_DATA( map2.get( key22 )->cStr(), "Entry2.2", 9 );
93  TS_ASSERT_SAME_DATA( map2.get( key23 )->cStr(), "Entry2.3", 9 );
94  TS_ASSERT_SAME_DATA( map2.get( key24 )->cStr(), "Entry2.4", 9 );
95  TS_ASSERT_EQUALS( map2.get( key25 ), ( void* )0 );
96 
97  while( map2.size() > 0 )
98  {
99  delete map2.popIndexed( 0 );
100  }
101 
102  }
103 
106  {
107  int n = 40000;
108  FlexArray<Str*> keys( true, n / 2 );
109  FastMap<Str, int*> map( false, n / 2 );
110  for( int i = 1; i < n; ++i )
111  {
112  Str* str = new Str( ( int )( random() % ( n / 2 ) ) );
113  if( !map.put( *str, &n ) )
114  {
115  delete str;
116  }
117  else
118  {
119  keys.push( str );
120  }
121  }
122 
123  for( unsigned int i = 0; i < keys.size(); ++i )
124  {
125  Str* key = keys.get( i );
126  TS_ASSERT_EQUALS( map.get( *key ), &n );
127  }
128  }
129 };
130 
131 #endif // _FASTMAP_TEST_H
const T & get(int index=-1) const
Definition: FlexArray.h:134
Simple class for providing key-pointer mappings.
Definition: Slate.h:44
void push(T item)
Definition: FlexArray.h:160
unsigned int size()
Definition: FastMap.h:159
void testPutGetAndSizeNoDuplicates(void)
FastMap Test - heap-allocated storage.
Definition: FastMap_Test.h:28
unsigned int size() const
Definition: FlexArray.cpp:69
void testPutGetAndSizeNoDuplicatesDynamic(void)
FastMap Test - dynamically-allocated storage.
Definition: FastMap_Test.h:71
Replacement for standard template class string.
Definition: Str.h:12
void testPutGetRandomDumpDuplicates(void)
FastMap Test - heap-allocated storage.
Definition: FastMap_Test.h:105
const char * cStr() const
Definition: Str.h:188
Contains the FastMap class declaration.
Units tests for class FastMap
Definition: FastMap_Test.h:23
T popIndexed(unsigned int index)
Definition: FastMap.h:106
T get(const S &key, bool andPop=false)
Definition: FastMap.h:66
bool put(S &key, T item, bool deleteOnCleanup=false)
Definition: FastMap.h:39