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


// file: test/lock_test.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 <fstream>
#include <iostream>
#include <shared_memory.h>

char key_val1[] = "/allocate_key";
char key_val2[] = "/allocate_key2";

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

int passed=0, failed=0;

// First, make sure that the shared memory segment are not in use
// during the test.
// shm_unlink(key_val1);
// shm_unlink(key_val2);

// mem_space::allocator_key_t key_val = "/tmp/.allocate_key";

// create the empty shm key file, used by ftok()
std::ofstream to1(key_val1);
if (!to1) perror("Cannot open key file");
to1.close();

std::ofstream to2(key_val2);
if (!to2) perror("Cannot open key file");
to2.close();


enum {
num_of_pages=1000,
page_size=8192,
bit_vector_size = 1000/8 + 1
};

std::cerr << "----------------------------------------" << std::endl;
std::cerr << "Lock class test. " << std::endl;

std::cerr << "\tConstructor test" << std::endl;
mem_space::locks my_lock1(key_val1,1);
mem_space::locks my_lock2(key_val2,1);
mem_space::locks my_lock3(key_val2,3);
std::cerr << "\tCopy Constructor test" << std::endl;
mem_space::locks my_lock4(my_lock1);
std::cerr << "\tAssignment Test" << std::endl;
mem_space::locks my_lock5 = my_lock3;
std::cerr << "\tEquality Test" << std::endl;
if (my_lock4==my_lock1)
passed++;
else
failed++;
if (!(my_lock4==my_lock3))
passed++;
else
failed++;
if (!(my_lock1==my_lock2))
passed++;
else
failed++;
my_lock1.print();
std::cerr << "my_lock1.lock(1)" << std::endl;
my_lock1.lock(1);
my_lock1.print();
std::cerr << "my_lock1.unlock(1)" << std::endl;
my_lock1.unlock(1);
my_lock1.print();
std::cerr << "my_lock4.lock(30)" << std::endl;
my_lock4.lock(30);
my_lock4.print();
std::cerr << "my_lock4.unlock(30)" << std::endl;
my_lock4.unlock(30);
my_lock4.print();
my_lock2.lock(2);
my_lock2.lock(3);

std::cerr << "\tDestructor test" << std::endl;
my_lock1.~locks();

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


return 0;
}