aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/options_TEST.cc
blob: ddf0ee7b0ead68ea4bb1df458f232b807898065b (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007, 2009, 2010, 2011 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/util/options.hh>

#include <gtest/gtest.h>

using namespace paludis;

namespace
{
    enum MyOption { mo_one, mo_two, mo_three, mo_four, mo_five, mo_six, mo_seven, mo_eight, mo_nine, mo_ten, last_mo };
    typedef Options<MyOption> MyOptions;
}

TEST(Options, Works)
{
    MyOptions options, second;

    EXPECT_TRUE(! options.any());

    for (MyOption o(static_cast<MyOption>(0)) ; o < last_mo ; o = static_cast<MyOption>(static_cast<long>(o) + 1))
        EXPECT_TRUE(! options[o]);

    for (MyOption o(static_cast<MyOption>(0)) ; o < last_mo ; o = static_cast<MyOption>(static_cast<long>(o) + 2))
        options = options + o;

    EXPECT_TRUE(options.any());

    for (MyOption o(static_cast<MyOption>(0)) ; o < last_mo ; o = static_cast<MyOption>(static_cast<long>(o) + 2))
        EXPECT_TRUE(options[o]);

    for (MyOption o(static_cast<MyOption>(1)) ; o < last_mo ; o = static_cast<MyOption>(static_cast<long>(o) + 2))
        EXPECT_TRUE(! options[o]);

    for (MyOption o(static_cast<MyOption>(0)) ; o < last_mo ; o = static_cast<MyOption>(static_cast<long>(o) + 1))
        second += o;

    options |= second;

    for (MyOption o(static_cast<MyOption>(0)) ; o < last_mo ; o = static_cast<MyOption>(static_cast<long>(o) + 1))
        EXPECT_TRUE(options[o]);

    for (MyOption o(static_cast<MyOption>(1)) ; o < last_mo ; o = static_cast<MyOption>(static_cast<long>(o) + 2))
        options -= o;

    EXPECT_TRUE(options.any());

    for (MyOption o(static_cast<MyOption>(0)) ; o < last_mo ; o = static_cast<MyOption>(static_cast<long>(o) + 2))
        options = options - o;

    EXPECT_TRUE(! options.any());

    second = { mo_three, mo_eight };
    options |= second;
    options += mo_seven;
    EXPECT_TRUE(options.any());
    options.subtract(second);
    EXPECT_TRUE(options.any());
    options -= mo_seven;
    EXPECT_TRUE(! options.any());
    EXPECT_TRUE(options.highest_bit() >= static_cast<int>(last_mo));
}

TEST(Options, Initialiser)
{
    MyOptions a({});
    EXPECT_TRUE(! a.any());

    MyOptions b({ mo_two, mo_four, mo_six });
    EXPECT_TRUE(! b[mo_one]);
    EXPECT_TRUE(b[mo_two]);
    EXPECT_TRUE(! b[mo_three]);
    EXPECT_TRUE(b[mo_four]);
    EXPECT_TRUE(! b[mo_five]);
    EXPECT_TRUE(b[mo_six]);
    EXPECT_TRUE(! b[mo_seven]);
}