summaryrefslogtreecommitdiff
path: root/src/test/cpp/helpers/cyclicbuffertestcase.cpp
blob: 05e90653b5ef1f1e6834a8e631bac3fd10d77e5f (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
163
164
165
166
167
168
169
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <log4cxx/helpers/cyclicbuffer.h>
#include "../logunit.h"

#include <log4cxx/logmanager.h>
#include <log4cxx/logger.h>
#include <log4cxx/spi/loggingevent.h>
#include <log4cxx/spi/location/locationinfo.h>
#include "../testchar.h"

using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::spi;

#define MAX 1000

LOGUNIT_CLASS(CyclicBufferTestCase)
{
   LOGUNIT_TEST_SUITE(CyclicBufferTestCase);
      LOGUNIT_TEST(test0);
      LOGUNIT_TEST(test1);
      LOGUNIT_TEST(testResize);
   LOGUNIT_TEST_SUITE_END();

   LoggerPtr logger;
   std::vector<LoggingEventPtr> e;

public:
   void setUp()
   {
      e.reserve(1000);
      LoggingEventPtr event;
      for (int i = 0; i < MAX; i++)
      {
         event = new LoggingEvent(LOG4CXX_STR("x"), Level::getDebug(), LOG4CXX_STR("e"),
                          log4cxx::spi::LocationInfo::getLocationUnavailable());
         e.push_back(event);
      }
   }

   void tearDown()
   {
      LogManager::shutdown();
   }

   void test0()
   {
      int size = 2;

      CyclicBuffer cb(size);
      LOGUNIT_ASSERT_EQUAL(size, cb.getMaxSize());

      cb.add(e[0]);
      LOGUNIT_ASSERT_EQUAL(1, cb.length());
      LOGUNIT_ASSERT_EQUAL(e[0], cb.get());
      LOGUNIT_ASSERT_EQUAL(0, cb.length());
      LOGUNIT_ASSERT(cb.get() == 0);
      LOGUNIT_ASSERT_EQUAL(0, cb.length());

      CyclicBuffer cb2(size);
      cb2.add(e[0]);
      cb2.add(e[1]);
      LOGUNIT_ASSERT_EQUAL(2, cb2.length());
      LOGUNIT_ASSERT_EQUAL(e[0], cb2.get());
      LOGUNIT_ASSERT_EQUAL(1, cb2.length());
      LOGUNIT_ASSERT_EQUAL(e[1], cb2.get());
      LOGUNIT_ASSERT_EQUAL(0, cb2.length());
      LOGUNIT_ASSERT(cb2.get() == 0);
      LOGUNIT_ASSERT_EQUAL(0, cb2.length());
   }

   void test1()
   {
      for (int bufSize = 1; bufSize <= 128; bufSize *= 2)
         doTest1(bufSize);
   }

   void doTest1(int size)
   {
      //System.out.println("Doing test with size = "+size);
      CyclicBuffer cb(size);

      LOGUNIT_ASSERT_EQUAL(size, cb.getMaxSize());

      int i;

      for (i = -(size + 10); i < (size + 10); i++)
      {
         LOGUNIT_ASSERT(cb.get(i) == 0);
      }

      for (i = 0; i < MAX; i++)
      {
         cb.add(e[i]);

         int limit = (i < (size - 1)) ? i : (size - 1);

         //System.out.println("\nLimit is " + limit + ", i="+i);
         for (int j = limit; j >= 0; j--)
         {
            //System.out.println("i= "+i+", j="+j);
            LOGUNIT_ASSERT_EQUAL(e[i - (limit - j)], cb.get(j));
         }

         LOGUNIT_ASSERT(cb.get(-1) == 0);
         LOGUNIT_ASSERT(cb.get(limit + 1) == 0);
      }
   }

   void testResize()
   {
      for (int isize = 1; isize <= 128; isize *= 2)
      {
         doTestResize(isize, (isize / 2) + 1, (isize / 2) + 1);
         doTestResize(isize, (isize / 2) + 1, isize + 10);
         doTestResize(isize, isize + 10, (isize / 2) + 1);
         doTestResize(isize, isize + 10, isize + 10);
      }

   }

   void doTestResize(int initialSize, int numberOfAdds, int newSize)
   {
      //System.out.println("initialSize = "+initialSize+", numberOfAdds="
      //        +numberOfAdds+", newSize="+newSize);
      CyclicBuffer cb(initialSize);

      for (int i = 0; i < numberOfAdds; i++)
      {
         cb.add(e[i]);
      }

      cb.resize(newSize);

      int offset = numberOfAdds - initialSize;

      if (offset < 0)
      {
         offset = 0;
      }

      int len = (newSize < numberOfAdds) ? newSize : numberOfAdds;
      len = (len < initialSize) ? len : initialSize;

      //System.out.println("Len = "+len+", offset="+offset);
      for (int j = 0; j < len; j++)
      {
         LOGUNIT_ASSERT_EQUAL(e[offset + j], cb.get(j));
      }
   }
};

LOGUNIT_TEST_SUITE_REGISTRATION(CyclicBufferTestCase);