PageRenderTime 25ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/kangmodb/testClassArrayMap.cpp

#
C++ | 144 lines | 74 code | 30 blank | 40 comment | 67 complexity | a21d3d2e3cef2d47e67d975b254530ef MD5 | raw file
Possible License(s): BSD-3-Clause
  1/*
  2 *  testClassArrayMap.cpp
  3 *  kangmodb
  4 *
  5 *  Created by 강모 김 on 11. 5. 8..
  6 *  Copyright 2011 강모소프트. All rights reserved.
  7 *
  8 */
  9
 10#include "gtest/gtest.h"
 11
 12#include "testCommon.h"
 13#include "types.h"
 14#include "kdInfra.h"
 15#include "table.h"	
 16#include "arrayMap.h"	
 17	
 18
 19/** @brief Test kdArrayMap class.
 20 */
 21class ArrayMapTest : public testing::Test {
 22protected:  // You should make the members protected s.t. they can be
 23	// accessed from sub-classes.
 24	
 25	// virtual void SetUp() will be called before each test is run.  You
 26	// should define it if you need to initialize the varaibles.
 27	// Otherwise, this can be skipped.
 28	virtual void SetUp() {
 29	}
 30	
 31	// virtual void TearDown() will be called after each test is run.
 32	// You should define it if there is cleanup work to do.  Otherwise,
 33	// you don't have to provide it.
 34	//
 35	virtual void TearDown() {
 36	}
 37protected:
 38	stgTable * table_;
 39};
 40
 41
 42
 43/** @brief Basic tests for ArrayMap.
 44 */
 45TEST_F(ArrayMapTest, Basic_GetKeysAssigned) {
 46	kdArrayMap<stgTableId, stgTable*> map;
 47	
 48	ASSERT_TRUE( map.initialize(3 /* max size */ ) == 0 );
 49	stgTable * table;
 50	stgTableId tableId;
 51	
 52	// TODO : Hit Assertion : put key 0.
 53#define PSEUDO_TBL_ADDR(n) ((stgTable*)(n*8))
 54	// Put 1st table
 55	ASSERT_TRUE( map.put(PSEUDO_TBL_ADDR(1), &tableId) == 0);
 56	ASSERT_TRUE( tableId == 1);
 57	ASSERT_TRUE( map.get(1, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(1) );
 58
 59	// Put 2nd table
 60	ASSERT_TRUE( map.put(PSEUDO_TBL_ADDR(2), &tableId) == 0);
 61	ASSERT_TRUE( tableId == 2);
 62	ASSERT_TRUE( map.get(1, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(1) );
 63	ASSERT_TRUE( map.get(2, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(2) );
 64
 65	// Put 3rd table
 66	ASSERT_TRUE( map.put(PSEUDO_TBL_ADDR(3), &tableId) == 0);
 67	ASSERT_TRUE( tableId == 3);
 68	ASSERT_TRUE( map.get(1, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(1) );
 69	ASSERT_TRUE( map.get(2, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(2) );
 70	ASSERT_TRUE( map.get(3, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(3) );
 71
 72	// Put 4th table => ArrayMap FULL.
 73	ASSERT_TRUE( map.put(PSEUDO_TBL_ADDR(4), &tableId) == KD_EXCP_ARRAY_MAP_FULL);
 74	
 75	// Remove the 2nd mapping
 76	ASSERT_TRUE( map.remove(2) == 0);
 77	ASSERT_TRUE( map.get(2, &table) == 0); ASSERT_TRUE( table == NULL );
 78	
 79	// Put 4th table
 80	ASSERT_TRUE( map.put(PSEUDO_TBL_ADDR(4), &tableId) == 0);
 81	// The removed tableId is reused.
 82	ASSERT_TRUE( tableId == 2);
 83	ASSERT_TRUE( map.get(2, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(4) );
 84
 85	// Remove tableID 1, 3
 86	ASSERT_TRUE( map.remove(1) == 0);
 87	ASSERT_TRUE( map.remove(3) == 0);
 88	ASSERT_TRUE( map.get(1, &table) == 0); ASSERT_TRUE( table == NULL );
 89	ASSERT_TRUE( map.get(3, &table) == 0); ASSERT_TRUE( table == NULL );
 90
 91	// Put 5th, 6th table, the table Id 1 and 3 are reused.
 92	ASSERT_TRUE( map.put(PSEUDO_TBL_ADDR(5), &tableId) == 0);
 93	ASSERT_TRUE( tableId == 1);
 94	ASSERT_TRUE( map.put(PSEUDO_TBL_ADDR(6), &tableId) == 0);
 95	ASSERT_TRUE( map.get(1, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(5) );
 96	ASSERT_TRUE( tableId == 3);
 97	ASSERT_TRUE( map.get(3, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(6) );
 98
 99	ASSERT_TRUE( map.destroy() == 0 );
100}
101
102
103/** @brief Basic tests for ArrayMap.
104 */
105TEST_F(ArrayMapTest, Basic_SetKey) {
106	kdArrayMap<stgTableId, stgTable*> map;
107	
108	ASSERT_TRUE( map.initialize(3 /* max size */ ) == 0 );
109	stgTable * table;
110	stgTableId tableId;
111	
112	// TODO : Hit Assertion : put key 0.
113#define PSEUDO_TBL_ADDR(n) ((stgTable*)(n*8))
114	// Put 1st table
115	ASSERT_TRUE( map.put(1, PSEUDO_TBL_ADDR(1)) == 0);
116	ASSERT_TRUE( map.get(1, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(1) );
117
118	// Put 2nd table
119	ASSERT_TRUE( map.put(2, PSEUDO_TBL_ADDR(2)) == 0);
120	ASSERT_TRUE( map.get(1, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(1) );
121	ASSERT_TRUE( map.get(2, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(2) );
122
123	// Put 3rd table
124	ASSERT_TRUE( map.put(3, PSEUDO_TBL_ADDR(3)) == 0);
125	ASSERT_TRUE( map.get(1, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(1) );
126	ASSERT_TRUE( map.get(2, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(2) );
127	ASSERT_TRUE( map.get(3, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(3) );
128
129	// Put 4th table => ArrayMap FULL.
130	ASSERT_TRUE( map.put(PSEUDO_TBL_ADDR(4), &tableId) == KD_EXCP_ARRAY_MAP_FULL);
131
132	// Remove the 2nd mapping
133	ASSERT_TRUE( map.remove(2) == 0);
134	ASSERT_TRUE( map.get(2, &table) == 0); ASSERT_TRUE( table == NULL );
135	
136	// Put 4th table
137	ASSERT_TRUE( map.put(PSEUDO_TBL_ADDR(4), &tableId) == 0);
138	// The removed tableId is reused.
139	ASSERT_TRUE( tableId == 2);
140	ASSERT_TRUE( map.get(2, &table) == 0); ASSERT_TRUE( table == PSEUDO_TBL_ADDR(4) );
141	
142	ASSERT_TRUE( map.destroy() == 0 );
143}
144