www.pudn.com > allocator.rar > map_with_string_index.cc


// file: test/map_with_string_index.cc
// author: Marc Bumble
// Mon Aug 11, 2003
// Memory allocator code for shared memory access
// Copyright (C) 2003 by Marc D. Bumble

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


#include <pooled_allocator.h>
#include <fstream> // for std::ofstream to()

char key_val1[] = "/allocate_key";

// const unsigned char key_addr = 0x0;
char key_addr1[] = "0x400d0000";


int main(int argc, char **argv) {

int passed=0, failed=0;

// make sure key shared memory segment is not in use
// shm_unlink(key_val1);

std::cerr << "----------------------------------------" << std::endl;
std::cerr << "Map with String index tests. " << std::endl;

typedef std::map<std::string,
int,
std::less<const std::string>,
pooled_allocator::Pool_alloc<std::pair<const std::string,
int>,
key_val1,
key_addr1> > map_t;

// create the map and add entries.

std::cerr << "Creating Map, adding entries" << std::endl;

std::string test = "Dilbert";
std::pair<std::string, int>
test_pr(test,0);
map_t temp_map;
temp_map.insert(test_pr);

test = "Ratbert";
test_pr = std::pair<std::string, int>(test,1);
temp_map.insert(test_pr);

test = "Catbert";
test_pr = std::pair<std::string, int>(test,2);
temp_map.insert(test_pr);

test = "Asok";
test_pr = std::pair<std::string, int>(test,3);
temp_map.insert(test_pr);

test = "Wally";
test_pr = std::pair<std::string, int>(test,4);
temp_map.insert(test_pr);

test = "Alice";
test_pr = std::pair<std::string, int>(test,5);
temp_map.insert(test_pr);

std::cerr << "Map entries complete" << std::endl;
for (map_t::iterator idx = temp_map.begin(); idx != temp_map.end(); idx++)
std::cerr << "\tkey: " << idx->first << "\tval: " << idx->second << std::endl;


// Copy Constructor test
std::cerr << "Map Copy Constructor test" << std::endl;
map_t copy_map(temp_map);

// Assignment Operator test
std::cerr << "Map Assignment Operator test" << std::endl;
map_t assign_map = temp_map;

std::string key;
int val;

//////////////////////////////////////////////////////////////////
// Original temp_map tests
//////////////////////////////////////////////////////////////////




// find Wally
std::cerr << "Map find() test" << std::endl;
test = "Wally";
map_t::iterator it = temp_map.find(test);
if (it != temp_map.end()) {
key = it->first;
val = it->second;
if ((key == test) &amt;&amt; (val == 4))
passed++;
else
failed++;
} else {
failed++;
}

// erase Wally
std::cerr << "Map erase() test" << std::endl;
temp_map.erase(it);

// find Alice
std::cerr << "Map find() test" << std::endl;
test = "Alice";
it = temp_map.find(test);
if (it != temp_map.end()) {
key = it->first;
val = it->second;
// std::cerr << "key: " << key << " \tval: " << val << std::endl;
if ((key == test) &amt;&amt; (val == 5))
passed++;
else
failed++;
} else {
failed++;
}

// find Wally
std::cerr << "Map find() test after erase()" << std::endl;
test = "Wally";
it = temp_map.find(test);
if (it != temp_map.end()) {
failed++;
} else {
passed++;
}

//////////////////////////////////////////////////////////////////
// Copy Construction of original copy_map
//////////////////////////////////////////////////////////////////

std::cerr << "Repeat tests for Copy Construction of original" << std::endl;
// find Wally
test = "Wally";
it = copy_map.find(test);
if (it != copy_map.end()) {
key = it->first;
val = it->second;
if ((key == test) &amt;&amt; (val == 4))
passed++;
else
failed++;
} else {
failed++;
}

// erase Wally
copy_map.erase(it);

// find Alice
test = "Alice";
it = copy_map.find(test);
if (it != copy_map.end()) {
key = it->first;
val = it->second;
// std::cerr << "key: " << key << " \tval: " << val << std::endl;
if ((key == test) &amt;&amt; (val == 5))
passed++;
else
failed++;
} else {
failed++;
}

// find Wally
test = "Wally";
it = copy_map.find(test);
if (it != copy_map.end()) {
failed++;
} else {
passed++;
}

//////////////////////////////////////////////////////////////////
// Assign Construction of original copy_map
//////////////////////////////////////////////////////////////////

std::cerr << "Repeat tests for Assignment Construction of original" << std::endl;
// find Wally
test = "Wally";
it = assign_map.find(test);
if (it != assign_map.end()) {
key = it->first;
val = it->second;
if ((key == test) &amt;&amt; (val == 4))
passed++;
else
failed++;
} else {
failed++;
}

// erase Wally
assign_map.erase(it);

// find Alice
test = "Alice";
it = assign_map.find(test);
if (it != assign_map.end()) {
key = it->first;
val = it->second;
// std::cerr << "key: " << key << " \tval: " << val << std::endl;
if ((key == test) &amt;&amt; (val == 5))
passed++;
else
failed++;
} else {
failed++;
}

// find Wally
test = "Wally";
it = assign_map.find(test);
if (it != assign_map.end()) {
failed++;
} else {
passed++;
}


//////////////////////////////////////////////////////////////////
// Equality Tests
//////////////////////////////////////////////////////////////////

std::cerr << "Run through three transitive equality tests" << std::endl;
if (copy_map == temp_map ) {
passed++;
} else {
failed++;
}

if (assign_map == temp_map) {
passed++;
} else {
failed++;
}

if (copy_map == assign_map) {
passed++;
} else {
failed++;
}

// pooled_allocator::Pool<int,key_val1,key_addr1>
// pool_obj;
// pool_obj.shutdown();


std::cerr << "Passed: " << passed << std::endl;
std::cerr << "Failed: " << failed << std::endl;
std::cerr << "----------------------------------------" << std::endl;

return 0;
}