tesseract  5.0.0-alpha-619-ge9db
qrsequence_test.cc
Go to the documentation of this file.
1 // (C) Copyright 2017, Google Inc.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 // http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software
7 // distributed under the License is distributed on an "AS IS" BASIS,
8 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 // See the License for the specific language governing permissions and
10 // limitations under the License.
11 
12 
13 #include <algorithm>
14 #include <vector>
15 
16 #include "cycletimer.h"
17 #include "include_gunit.h"
18 #include "log.h"
19 #include "qrsequence.h"
20 
21 namespace {
22 
23 class TestableQRSequenceGenerator : public QRSequenceGenerator {
24  public:
25  explicit TestableQRSequenceGenerator(const int& N) : QRSequenceGenerator(N) {}
26  // Overriding scope for testing
28 };
29 
30 // Verifies binary inversion for a small range.
31 TEST(QRSequenceGenerator, GetBinaryReversedInteger) {
32  const int kRangeSize = 8;
33  TestableQRSequenceGenerator generator(kRangeSize);
34  int reversed_vals[kRangeSize] = {0, 4, 2, 6, 1, 5, 3, 7};
35  for (int i = 0; i < kRangeSize; ++i)
36  EXPECT_EQ(reversed_vals[i], generator.GetBinaryReversedInteger(i));
37 }
38 
39 // Trivial test fixture for a parameterized test.
40 class QRSequenceGeneratorTest : public ::testing::TestWithParam<int> {
41  protected:
42  void SetUp() {
43  std::locale::global(std::locale(""));
44  }
45 };
46 
47 TEST_P(QRSequenceGeneratorTest, GeneratesValidSequence) {
48  const int kRangeSize = GetParam();
49  TestableQRSequenceGenerator generator(kRangeSize);
50  std::vector<int> vals(kRangeSize);
51  CycleTimer timer;
52  timer.Restart();
53  for (int i = 0; i < kRangeSize; ++i) vals[i] = generator.GetVal();
54  LOG(INFO) << kRangeSize << "-length sequence took " << timer.GetInMs() << "ms";
55  // Sort the numbers to verify that we've covered the range without repetition.
56  std::sort(vals.begin(), vals.end());
57  for (int i = 0; i < kRangeSize; ++i) {
58  EXPECT_EQ(i, vals[i]);
59  if (i != vals[i]) {
60  LOG(INFO) << "Aborting remaining comparisons";
61  break;
62  }
63  }
64 }
65 
66 // Run a parameterized test using the following range sizes.
67 INSTANTIATE_TEST_CASE_P(RangeTest, QRSequenceGeneratorTest,
68  ::testing::Values(2, 7, 8, 9, 16, 1e2, 1e4, 1e6));
69 } // namespace
INFO
Definition: log.h:29
QRSequenceGenerator
Definition: qrsequence.h:32
include_gunit.h
CycleTimer::GetInMs
int64_t GetInMs() const
Definition: cycletimer.h:48
cycletimer.h
QRSequenceGenerator::GetBinaryReversedInteger
int GetBinaryReversedInteger(int in_val) const
Definition: qrsequence.h:58
CycleTimer
Definition: cycletimer.h:19
CycleTimer::Restart
void Restart()
Definition: cycletimer.h:37
qrsequence.h
log.h
LOG
Definition: cleanapi_test.cc:19