Four new quotes from around the world.
[alexxy/gromacs.git] / src / gromacs / mdlib / tests / bitmask.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief
37  * Tests for bitmask functionality.
38  *
39  * These tests check the functionality of bitmask.h
40
41  * \author Roland Schulz <roland@rschulz.eu>
42  */
43 #include <gtest/gtest.h>
44
45 #include "gromacs/mdlib/bitmask.h"
46
47 //! Implemenation of BITMASK_CLASSNAME
48 #define BITMASK_CLASSNAME_(S) BitmaskTest ## S
49 //! Returns name of Bitmask test fixture class
50 #define BITMASK_CLASSNAME(S) BITMASK_CLASSNAME_(S)
51 //! Implementation of BITMASK_TEST_P
52 #define BITMASK_TEST_P_(C, T) TEST_P(C, T)
53 //! Defines a parameterized bitmask test
54 #define BITMASK_TEST_P(T) BITMASK_TEST_P_(BITMASK_CLASSNAME(BITMASK_SIZE), T)
55
56 class BITMASK_CLASSNAME(BITMASK_SIZE) : public ::testing::TestWithParam<int>
57 {
58 };
59
60 BITMASK_TEST_P(SetAndClear)
61 {
62     gmx_bitmask_t m;
63     int           i = GetParam();
64     bitmask_clear(&m);
65     EXPECT_TRUE(bitmask_is_zero(m));
66     EXPECT_FALSE(bitmask_is_set(m, i));
67     bitmask_set_bit(&m, i);
68     for (int j = 0; j < BITMASK_SIZE; j++)
69     {
70         EXPECT_EQ(bitmask_is_set(m, j), j == i);
71     }
72     bitmask_clear(&m);
73     EXPECT_TRUE(bitmask_is_zero(m));
74 }
75
76 BITMASK_TEST_P(InitBit)
77 {
78     gmx_bitmask_t m1, m2;
79     int           i = GetParam();
80     bitmask_init_bit(&m1, i);
81     bitmask_clear(&m2);
82     EXPECT_FALSE(bitmask_is_equal(m1, m2));
83     bitmask_set_bit(&m2, i);
84     EXPECT_TRUE(bitmask_is_equal(m1, m2));
85 }
86
87 BITMASK_TEST_P(InitLowBits)
88 {
89     gmx_bitmask_t m;
90     int           i = GetParam();
91     bitmask_init_low_bits(&m, i);
92     for (int j = 0; j < BITMASK_SIZE; j++)
93     {
94         EXPECT_EQ(bitmask_is_set(m, j), j < i);
95     }
96 }
97
98 BITMASK_TEST_P(Disjoint)
99 {
100     gmx_bitmask_t m1, m2;
101     int           i = GetParam();
102     bitmask_init_bit(&m1, i);
103     bitmask_init_bit(&m2, i);
104     EXPECT_FALSE(bitmask_is_disjoint(m1, m2));
105     bitmask_init_low_bits(&m2, i);
106     EXPECT_TRUE(bitmask_is_disjoint(m1, m2));
107 }
108
109 BITMASK_TEST_P(Union)
110 {
111     gmx_bitmask_t m1, m2;
112     int           i = GetParam();
113     int           j = (i + BITMASK_SIZE/2)%BITMASK_SIZE;
114     bitmask_init_bit(&m1, i);
115     bitmask_init_bit(&m2, j);
116     bitmask_union(&m1, m2);
117     for (int k = 0; k < BITMASK_SIZE; k++)
118     {
119         EXPECT_EQ(bitmask_is_set(m1, k), k == i || k == j);
120     }
121
122     bitmask_init_bit(&m1, i);
123     bitmask_clear(&m2);
124     bitmask_union(&m1, m2);
125     bitmask_init_bit(&m2, i);
126     EXPECT_TRUE(bitmask_is_equal(m1, m2));
127
128     bitmask_clear(&m1);
129     bitmask_init_bit(&m2, i);
130     bitmask_union(&m1, m2);
131     EXPECT_TRUE(bitmask_is_equal(m1, m2));
132 }