Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bounded_sequence_encoding_test.cc « interpolate « lm - github.com/kpu/kenlm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a5be9ecdbe493557e57494d34d3b0325f944f8d (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
#include "bounded_sequence_encoding.hh"

#include "../../util/scoped.hh"

#define BOOST_TEST_MODULE BoundedSequenceEncodingTest
#include <boost/test/unit_test.hpp>

namespace lm {
namespace interpolate {
namespace {

BOOST_AUTO_TEST_CASE(Simple) {
  unsigned char bounds[] = {2};
  BoundedSequenceEncoding enc(bounds, bounds + 1);
  util::scoped_malloc backing(util::MallocOrThrow(enc.EncodedLength()));
  unsigned char input = 1;
  enc.Encode(&input, backing.get());
  unsigned char output;
  enc.Decode(backing.get(), &output);
  BOOST_CHECK_EQUAL(1, output);
}

void ExhaustiveTest(unsigned char *bound_begin, unsigned char *bound_end) {
  BoundedSequenceEncoding enc(bound_begin, bound_end);
  util::scoped_malloc backing(util::MallocOrThrow(enc.EncodedLength()));
  std::vector<unsigned char> values(bound_end - bound_begin),
      out(bound_end - bound_begin);
  while (true) {
    enc.Encode(&values[0], backing.get());
    enc.Decode(backing.get(), &out[0]);
    for (std::size_t i = 0; i != values.size(); ++i) {
      BOOST_CHECK_EQUAL(values[i], out[i]);
    }
    for (std::size_t i = 0;; ++i) {
      if (i == values.size()) return;
      ++values[i];
      if (values[i] < bound_begin[i]) break;
      values[i] = 0;
    }
  }
}

void CheckEncodeDecode(unsigned char *bounds, unsigned char *input,
                       unsigned char *output, std::size_t len) {
  BoundedSequenceEncoding encoder(bounds, bounds + len);
  util::scoped_malloc backing(util::MallocOrThrow(encoder.EncodedLength()));

  encoder.Encode(input, backing.get());
  encoder.Decode(backing.get(), output);

  for (std::size_t i = 0; i < len; ++i) {
    BOOST_CHECK_EQUAL(input[i], output[i]);
  }
}

BOOST_AUTO_TEST_CASE(Exhaustive) {
  unsigned char bounds[] = {5, 2, 3, 9, 7, 20, 8};
  ExhaustiveTest(bounds, bounds + sizeof(bounds) / sizeof(unsigned char));
}

BOOST_AUTO_TEST_CASE(LessThan64) {
  unsigned char bounds[] = {255, 255, 255, 255, 255, 255, 255, 3};
  unsigned char input[] = {172, 183, 254, 187, 96, 87, 65, 2};
  unsigned char output[] = {0, 0, 0, 0, 0, 0, 0, 0};

  std::size_t len = sizeof(bounds) / sizeof(unsigned char);
  assert(sizeof(input) / sizeof(unsigned char) == len);
  assert(sizeof(output) / sizeof(unsigned char) == len);

  CheckEncodeDecode(bounds, input, output, len);
}

BOOST_AUTO_TEST_CASE(Exactly64) {
  unsigned char bounds[] = {255, 255, 255, 255, 255, 255, 255, 255};
  unsigned char input[] = {172, 183, 254, 187, 96, 87, 65, 16};
  unsigned char output[] = {0, 0, 0, 0, 0, 0, 0, 0};

  std::size_t len = sizeof(bounds) / sizeof(unsigned char);
  assert(sizeof(input) / sizeof(unsigned char) == len);
  assert(sizeof(output) / sizeof(unsigned char) == len);

  CheckEncodeDecode(bounds, input, output, len);
}

BOOST_AUTO_TEST_CASE(MoreThan64) {
  unsigned char bounds[] = {255, 255, 255, 255, 255, 255, 255, 255, 255};
  unsigned char input[] = {172, 183, 254, 187, 96, 87, 65, 16, 137};
  unsigned char output[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};

  std::size_t len = sizeof(bounds) / sizeof(unsigned char);
  assert(sizeof(input) / sizeof(unsigned char) == len);
  assert(sizeof(output) / sizeof(unsigned char) == len);

  CheckEncodeDecode(bounds, input, output, len);
}

}}} // namespaces