File: policy_tests.cpp

package info (click to toggle)
gringo 5.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,128 kB
  • sloc: cpp: 210,867; ansic: 37,507; python: 11,271; yacc: 825; javascript: 627; sh: 368; xml: 364; makefile: 102
file content (97 lines) | stat: -rw-r--r-- 3,346 bytes parent folder | download
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
/**
 * MIT License
 *
 * Copyright (c) 2017 Thibaut Goetghebuer-Planchon <tessil@gmx.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <tsl/sparse_growth_policy.h>

#include <boost/mpl/list.hpp>
#include <boost/test/unit_test.hpp>
#include <cstddef>
#include <limits>
#include <ratio>
#include <stdexcept>

#include "utils.h"

BOOST_AUTO_TEST_SUITE(test_policy)

using test_types =
    boost::mpl::list<tsl::sh::power_of_two_growth_policy<2>,
                     tsl::sh::power_of_two_growth_policy<4>,
                     tsl::sh::prime_growth_policy, tsl::sh::mod_growth_policy<>,
                     tsl::sh::mod_growth_policy<std::ratio<7, 2>>>;

BOOST_AUTO_TEST_CASE_TEMPLATE(test_policy, Policy, test_types) {
  // Call next_bucket_count() on the policy until we reach its
  // max_bucket_count()
  std::size_t bucket_count = 0;
  Policy policy(bucket_count);

  BOOST_CHECK_EQUAL(policy.bucket_for_hash(0), 0);
  BOOST_CHECK_EQUAL(bucket_count, 0);

#if !TSL_NO_EXCEPTIONS
  bool exception_thrown = false;
  try {
    while (true) {
      const std::size_t previous_bucket_count = bucket_count;

      bucket_count = policy.next_bucket_count();
      policy = Policy(bucket_count);

      BOOST_CHECK_EQUAL(policy.bucket_for_hash(0), 0);
      BOOST_CHECK(bucket_count > previous_bucket_count);
    }
  } catch (const std::length_error&) {
    exception_thrown = true;
  }

  BOOST_CHECK(exception_thrown);
#endif
}

BOOST_AUTO_TEST_CASE_TEMPLATE(test_policy_min_bucket_count, Policy,
                              test_types) {
  // Check policy when a bucket_count of 0 is asked.
  std::size_t bucket_count = 0;
  Policy policy(bucket_count);

  BOOST_CHECK_EQUAL(policy.bucket_for_hash(0), 0);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(test_policy_max_bucket_count, Policy,
                              test_types) {
  // Test a bucket_count equals to the max_bucket_count limit and above
  std::size_t bucket_count = 0;
  Policy policy(bucket_count);

  bucket_count = policy.max_bucket_count();
  Policy policy2(bucket_count);

  bucket_count = std::numeric_limits<std::size_t>::max();
  TSL_SH_CHECK_THROW((Policy(bucket_count)), std::length_error);

  bucket_count = policy.max_bucket_count() + 1;
  TSL_SH_CHECK_THROW((Policy(bucket_count)), std::length_error);
}

BOOST_AUTO_TEST_SUITE_END()