summaryrefslogtreecommitdiff
path: root/tests/Mem_Map_Test.cpp
blob: ff5f2cd84c6e85487f723847167dce343c5171be (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
// 
// = FILENAME
//    Mem_Map_Test.cpp
//
// = DESCRIPTION
//      This test illustrates the use of ACE_Mem_Map to reverse a
//      file. The test first creates a dummy file for testing, then
//      reverses the file and then reverses it again to get back the
//      original file. 
//
// = AUTHOR
//    Prashant Jain
// 
// ============================================================================


#include "ace/Mem_Map.h"
#include "test_config.h"

static const int FILE_SIZE = 10;
static const int NUM_LINES = 15;

static void
reverse_file (ACE_HANDLE file_handle,
	      char *array, 
	      int size)
{
  int count = 0;
  size--;

  if (array[size] == '\0')
    array[size] = '\n';

  while (--size >= 0)
    {
      if (array[size] == '\n')
	{
	  ACE_OS::write (file_handle, array + size + 1, count);
	  ACE_OS::write (file_handle, "\n", 1);
	  count = 0;
	}
      else
	count++;
    }
  ACE_OS::write (file_handle, array, count+1);
}

static int
create_test_file (int size, int num_lines)
{
  char *mybuf;
  
  ACE_NEW_RETURN (mybuf, char[size + 1], -1);
  char c = 'a';
  char d = c;

  ACE_HANDLE file_handle = ACE_OS::open (ACE_DEFAULT_TEST_FILE, 
					 O_RDWR | O_CREAT | O_TRUNC,
					 0666);

  if (file_handle  == ACE_INVALID_HANDLE)
    ACE_ERROR_RETURN ((LM_ERROR, "Open failed\n"), -1);
  
  for (int j = 0; j < num_lines; j++)
    {
      for (int i = 0; i < size; i++)
	{
	  mybuf[i] = c;
	  c++;
	}

      mybuf[size] = '\0';

      c = ++d;

      if (ACE_OS::write (file_handle, mybuf, size) != size)
	ACE_ERROR_RETURN ((LM_ERROR, "write to file failed\n"), -1);

      if (ACE_OS::write (file_handle, "\n", 1) != 1)
	ACE_ERROR_RETURN ((LM_ERROR, "write to file failed\n"), -1);
    }

  ACE_OS::close (file_handle);
  return 0;
}

int
main (int, char *[])
{
  ACE_START_TEST ("Mem_Map_Test");

  // First create a test file to work on
  if (create_test_file (FILE_SIZE, NUM_LINES) != 0)
    ACE_ERROR_RETURN ((LM_ERROR, "Create test file failed\n"), -1);

  ACE_Mem_Map mmap;
  
  // First memory map the test file
  if (mmap.map (ACE_DEFAULT_TEST_FILE) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%n: %p\n%a", "mmap"), -1);

  ACE_HANDLE temp_file_handle = ACE_OS::open (ACE_TEMP_FILE_NAME, 
					      O_RDWR | O_TRUNC | O_CREAT,
					      0666);

  // Now create a temporary file for intermediate processing
  if (temp_file_handle == ACE_INVALID_HANDLE)
    ACE_ERROR_RETURN ((LM_ERROR, "Open failed\n"), -1);

  // Reverse the original file and write the output to the temporary
  // file.
  reverse_file (temp_file_handle, 
		(char *) mmap.addr (), 
		mmap.size ());

  ACE_OS::close (temp_file_handle);

  ACE_Mem_Map temp_mmap;
  
  // Now memory map the temporary file
  if (temp_mmap.map (ACE_TEMP_FILE_NAME) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%n: %p\n%a", "mmap"), -1);

  TCHAR temp_file_name[BUFSIZ];

  // Create another temporary file that would hold the output of
  // reversing the first temporary file
  ACE_OS::sprintf (temp_file_name, __TEXT ("%s%s"), ACE_TEMP_FILE_NAME, __TEXT ("2"));
  if ((temp_file_handle = ACE_OS::open (temp_file_name, 
					O_RDWR | O_TRUNC | O_CREAT,
					0666)) == ACE_INVALID_HANDLE)
    ACE_ERROR_RETURN ((LM_ERROR, "Open failed\n"), -1);
  
  // Now reverse the temporary file and write everything to the second
  // temporary file.
  reverse_file (temp_file_handle, 
		(char *) temp_mmap.addr (), 
		temp_mmap.size ());

  ACE_OS::close (temp_file_handle);

  // Memory map the second temporary file
  ACE_Mem_Map temp_mmap2;

  if (temp_mmap2.map (temp_file_name) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%n: %p\n%a", "mmap"), -1);

  // Now do a memcmp -- the orig file and the second temporary file
  // should be identical.
  ACE_ASSERT (ACE_OS::memcmp (temp_mmap2.addr (), 
			      mmap.addr (),
			      mmap.size ()) == 0);

  ACE_END_TEST;
  return 0;
}