Line data Source code
1 : // Copyright (c) 2026 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/magpie
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : // self
20 : //
21 : #include "catch_main.h"
22 :
23 :
24 : // magpie
25 : //
26 : #include <magpie/options.h>
27 :
28 : #include <magpie/exception.h>
29 :
30 :
31 : // last include
32 : //
33 : #include <snapdev/poison.h>
34 :
35 :
36 :
37 2 : CATCH_TEST_CASE("options", "[options]")
38 : {
39 2 : CATCH_START_SECTION("options: verify options defaults")
40 : {
41 1 : magpie::options opts;
42 :
43 1 : CATCH_REQUIRE_FALSE(opts.get_least_recently_used());
44 1 : CATCH_REQUIRE_FALSE(opts.get_least_frequently_used());
45 1 : CATCH_REQUIRE_FALSE(opts.get_allow_empty_key());
46 1 : CATCH_REQUIRE(opts.get_capacity() == 0);
47 1 : CATCH_REQUIRE(opts.get_count() == 0);
48 1 : CATCH_REQUIRE(opts.get_default_ttl() == magpie::NO_TTL);
49 : }
50 2 : CATCH_END_SECTION()
51 :
52 2 : CATCH_START_SECTION("options: verify options can be changed")
53 : {
54 1 : magpie::options opts;
55 :
56 : {
57 1 : CATCH_REQUIRE_FALSE(opts.get_least_recently_used());
58 1 : opts.set_least_recently_used(true);
59 1 : CATCH_REQUIRE(opts.get_least_recently_used());
60 1 : opts.set_least_recently_used(false);
61 1 : CATCH_REQUIRE_FALSE(opts.get_least_recently_used());
62 : }
63 :
64 : {
65 1 : CATCH_REQUIRE_FALSE(opts.get_least_frequently_used());
66 1 : opts.set_least_frequently_used(true);
67 1 : CATCH_REQUIRE(opts.get_least_frequently_used());
68 1 : opts.set_least_frequently_used(false);
69 1 : CATCH_REQUIRE_FALSE(opts.get_least_frequently_used());
70 : }
71 :
72 : {
73 1 : CATCH_REQUIRE_FALSE(opts.get_allow_empty_key());
74 1 : opts.set_allow_empty_key(true);
75 1 : CATCH_REQUIRE(opts.get_allow_empty_key());
76 1 : opts.set_allow_empty_key(false);
77 1 : CATCH_REQUIRE_FALSE(opts.get_allow_empty_key());
78 : }
79 :
80 2004 : for(std::size_t capacity(0); capacity < 1'000'000; capacity += rand() % 1'000)
81 : {
82 2003 : opts.set_capacity(capacity);
83 2003 : CATCH_REQUIRE(opts.get_capacity() == capacity);
84 : }
85 :
86 1998 : for(std::size_t count(0); count < 1'000'000; count += rand() % 1'000)
87 : {
88 1997 : opts.set_count(count);
89 1997 : CATCH_REQUIRE(opts.get_count() == count);
90 : }
91 :
92 2026 : for(magpie::duration_t duration(1); duration < 1'000'000; duration += rand() % 1'000)
93 : {
94 2025 : opts.set_default_ttl(duration);
95 2025 : CATCH_REQUIRE(opts.get_default_ttl() == duration);
96 : }
97 :
98 : // specifically test the special values
99 : {
100 1 : opts.set_default_ttl(magpie::NO_TTL);
101 1 : CATCH_REQUIRE(opts.get_default_ttl() == magpie::NO_TTL);
102 :
103 1 : opts.set_default_ttl(magpie::DEFAULT_TTL);
104 1 : CATCH_REQUIRE(opts.get_default_ttl() == magpie::DEFAULT_TTL);
105 : }
106 : }
107 2 : CATCH_END_SECTION()
108 2 : }
109 :
110 :
111 1 : CATCH_TEST_CASE("options_error", "[options][error]")
112 : {
113 1 : CATCH_START_SECTION("options_error: verify that the TTL cannot be a negative number")
114 : {
115 1 : magpie::options opts;
116 :
117 99 : for(magpie::duration_t duration(-2); duration > -100; --duration)
118 : {
119 294 : CATCH_REQUIRE_THROWS_MATCHES(
120 : opts.set_default_ttl(duration)
121 : , magpie::out_of_range
122 : , Catch::Matchers::ExceptionMessage(
123 : "out_of_range: options::set_default_ttl(): ttl must be a positive number, magpie::NO_TTL, or magpie::DEFAULT_TTL."));
124 : }
125 : }
126 1 : CATCH_END_SECTION()
127 1 : }
128 :
129 :
130 : // vim: ts=4 sw=4 et
|