summaryrefslogtreecommitdiff
path: root/ACE/examples/Map_Manager/test_hash_map_manager.cpp
blob: b3324a46238a37997027a58502a831f37251338f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// $Id$

#include "ace/OS_main.h"
#include "ace/Hash_Map_Manager.h"
#include "ace/ACE.h"
#include "ace/SString.h"
#include "ace/Synch.h"
#include "ace/Synch_Traits.h"
#include "ace/OS_NS_stdio.h"
#include "ace/os_include/os_assert.h"



const int MAX_KEY_LEN = 1000;

typedef ACE_Hash_Map_Manager<ACE_TString, ACE_TString, ACE_SYNCH_RW_MUTEX> MAP_MANAGER;

int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  if (argc != 4)
    ACE_ERROR_RETURN ((LM_ERROR, "usage: %s tablesize file1 file2\n",
                       argv[0]), -1);
  else
    {
      int total = ACE_OS::atoi (argv[1]);

      if (!ACE_OS::freopen (argv[2], ACE_TEXT("r"), stdin))
        ACE_OS::perror (argv[0]), ACE_OS::exit (1);

      MAP_MANAGER hash (total);

      ACE_TCHAR key[MAX_KEY_LEN];

      while (ACE_OS::fgets (key, sizeof key, stdin))
        // stream ops are just too slow!!
        {
          ACE_TString string (key);
          hash.bind (string, string);
        }

      ACE_OS::fclose (stdin);

      MAP_MANAGER::ITERATOR iterator (hash);

      for (MAP_MANAGER::ENTRY *entry = 0;
           iterator.next (entry) != 0;
           iterator.advance ())
        ACE_DEBUG ((LM_DEBUG, "%s %s\n",
                    entry->ext_id_.fast_rep (),
                    entry->int_id_.fast_rep ()));

      if (!ACE_OS::freopen (argv[3], ACE_TEXT("r"), stdin))
        ACE_OS::perror (argv[0]), ACE_OS::exit (1);

      while (ACE_OS::fgets (key, sizeof key, stdin))
        {
          ACE_TString name (key);
          ACE_TString value;
          assert (hash.find (name, value) != -1);
          assert (name == value);
        }

      if (!ACE_OS::freopen (argv[3], ACE_TEXT("r"), stdin))
        ACE_OS::perror (argv[0]), ACE_OS::exit (1);

      while (ACE_OS::fgets (key, sizeof key, stdin))
        {
          ACE_TString string (key);
          assert (hash.unbind (string) != -1);
        }

      assert (hash.current_size () == 0);

      ACE_OS::fclose (stdin);
    }

  return 0;
}