~alan-griffiths/mir/fix-1654023

« back to all changes in this revision

Viewing changes to tests/unit-tests/test_flags.cpp

  • Committer: Daniel van Vugt
  • Date: 2015-04-28 07:54:10 UTC
  • mfrom: (2517 development-branch)
  • mto: This revision was merged to the branch mainline in revision 2673.
  • Revision ID: daniel.van.vugt@canonical.com-20150428075410-rwskshfuar7voesp
Merge latest trunk and fix conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored By: Andreas Pokorny <andreas.pokorny@canonical.com>
 
17
 */
 
18
 
 
19
#include "mir/flags.h"
 
20
#include "mir_toolkit/common.h"
 
21
 
 
22
#include <gtest/gtest.h>
 
23
#include <gmock/gmock.h>
 
24
 
 
25
namespace arbitrary_namespace
 
26
{
 
27
enum class Character : uint32_t
 
28
{
 
29
    Empty = 0,
 
30
    Logical = 1<<0,
 
31
    Idealistic = 1<<1,
 
32
    Englihtened = 1<<2,
 
33
    Curious = 1<<3,
 
34
    Paranoid = 1<<16,
 
35
    Unstable = 1<<17,
 
36
    Reckless = 1<<18,
 
37
    Positive = (1<<4) - 1,
 
38
    Negative = 0xFFFF0000
 
39
};
 
40
 
 
41
Character mir_enable_enum_bit_operators(Character);
 
42
using Profile = mir::Flags<Character>;
 
43
}
 
44
 
 
45
namespace mir
 
46
{
 
47
namespace ns_inside_mir
 
48
{
 
49
enum class Capability : uint8_t { Pointer = 1<<4, Touchpad = 1<<3};
 
50
Capability mir_enable_enum_bit_operators(Capability);
 
51
using Capabilities = mir::Flags<Capability>;
 
52
}
 
53
}
 
54
 
 
55
namespace nested = mir::ns_inside_mir;
 
56
namespace arb = arbitrary_namespace;
 
57
 
 
58
TEST(MirFlags,lookup_rules_work_in_mir_nested_namespace)
 
59
{
 
60
    using namespace testing;
 
61
    nested::Capabilities cap = nested::Capability::Pointer | nested::Capability::Touchpad;
 
62
 
 
63
    EXPECT_THAT(cap.value(), (1<<3) | (1<<4));
 
64
}
 
65
 
 
66
TEST(MirFlags,lookup_rules_work_in_arbitrary_namespace)
 
67
{
 
68
    using namespace testing;
 
69
    arb::Profile empty = arb::Character::Curious & arb::Character::Reckless;
 
70
 
 
71
    EXPECT_THAT(empty.value(),Eq(0));
 
72
}
 
73
 
 
74
TEST(MirFlags,contains_check_works_for_masks)
 
75
{
 
76
    using namespace testing;
 
77
    arb::Profile mostly_positive;
 
78
    mostly_positive |= arb::Character::Curious | arb::Character::Logical;
 
79
 
 
80
    EXPECT_THAT(contains(mostly_positive,arb::Character::Positive),Eq(false));
 
81
    mostly_positive = mostly_positive | arb::Character::Idealistic | arb::Character::Englihtened;
 
82
 
 
83
    EXPECT_THAT(contains(mostly_positive,arb::Character::Positive),Eq(true));
 
84
}
 
85
 
 
86
TEST(MirFlags, toggling_bits)
 
87
{
 
88
    using namespace testing;
 
89
    arb::Profile negative{arb::Character::Negative};
 
90
 
 
91
    EXPECT_THAT(contains(negative,arb::Character::Paranoid),Eq(true));
 
92
    EXPECT_THAT(contains(negative^arb::Character::Paranoid,arb::Character::Paranoid),Eq(false));
 
93
}