2011-11-21 01:43:20 +04:00
/*
* Copyright ( C ) 2010 Red Hat , Inc . All rights reserved .
*
* This file is part of LVM2 .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU General Public License v .2 .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software Foundation ,
2016-01-21 13:49:46 +03:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
2011-11-21 01:43:20 +04:00
*/
2015-11-27 12:54:57 +03:00
# include "units.h"
2018-04-26 13:59:39 +03:00
# include "libdevmapper.h"
2011-11-21 01:43:20 +04:00
enum {
NR_BITS = 137
} ;
2018-04-26 13:59:39 +03:00
static void * _mem_init ( void ) {
struct dm_pool * mem = dm_pool_create ( " bitset test " , 1024 ) ;
if ( ! mem ) {
fprintf ( stderr , " out of memory \n " ) ;
exit ( 1 ) ;
}
2011-11-21 01:43:20 +04:00
2018-04-26 13:59:39 +03:00
return mem ;
2011-11-21 01:43:20 +04:00
}
2018-04-26 13:59:39 +03:00
static void _mem_exit ( void * mem )
{
2011-11-21 01:43:20 +04:00
dm_pool_destroy ( mem ) ;
}
2018-04-26 13:59:39 +03:00
static void test_get_next ( void * fixture )
2011-11-21 01:43:20 +04:00
{
2018-04-26 13:59:39 +03:00
struct dm_pool * mem = fixture ;
2011-11-21 17:15:40 +04:00
int i , j , last = 0 , first ;
2011-11-21 01:43:20 +04:00
dm_bitset_t bs = dm_bitset_create ( mem , NR_BITS ) ;
for ( i = 0 ; i < NR_BITS ; i + + )
2018-04-26 13:59:39 +03:00
T_ASSERT ( ! dm_bit ( bs , i ) ) ;
2011-11-21 01:43:20 +04:00
for ( i = 0 , j = 1 ; i < NR_BITS ; i + = j , j + + )
dm_bit_set ( bs , i ) ;
first = 1 ;
for ( i = 0 , j = 1 ; i < NR_BITS ; i + = j , j + + ) {
if ( first ) {
last = dm_bit_get_first ( bs ) ;
first = 0 ;
} else
last = dm_bit_get_next ( bs , last ) ;
2018-04-26 13:59:39 +03:00
T_ASSERT ( last = = i ) ;
2011-11-21 01:43:20 +04:00
}
2018-04-26 13:59:39 +03:00
T_ASSERT ( dm_bit_get_next ( bs , last ) = = - 1 ) ;
2011-11-21 01:43:20 +04:00
}
static void bit_flip ( dm_bitset_t bs , int bit )
{
int old = dm_bit ( bs , bit ) ;
if ( old )
dm_bit_clear ( bs , bit ) ;
else
dm_bit_set ( bs , bit ) ;
}
2018-04-26 13:59:39 +03:00
static void test_equal ( void * fixture )
2011-11-21 01:43:20 +04:00
{
2018-04-26 13:59:39 +03:00
struct dm_pool * mem = fixture ;
2011-11-21 01:43:20 +04:00
dm_bitset_t bs1 = dm_bitset_create ( mem , NR_BITS ) ;
dm_bitset_t bs2 = dm_bitset_create ( mem , NR_BITS ) ;
int i , j ;
for ( i = 0 , j = 1 ; i < NR_BITS ; i + = j , j + + ) {
dm_bit_set ( bs1 , i ) ;
dm_bit_set ( bs2 , i ) ;
}
2018-04-26 13:59:39 +03:00
T_ASSERT ( dm_bitset_equal ( bs1 , bs2 ) ) ;
T_ASSERT ( dm_bitset_equal ( bs2 , bs1 ) ) ;
2011-11-21 01:43:20 +04:00
for ( i = 0 ; i < NR_BITS ; i + + ) {
bit_flip ( bs1 , i ) ;
2018-04-26 13:59:39 +03:00
T_ASSERT ( ! dm_bitset_equal ( bs1 , bs2 ) ) ;
T_ASSERT ( ! dm_bitset_equal ( bs2 , bs1 ) ) ;
2011-11-21 01:43:20 +04:00
2018-04-26 13:59:39 +03:00
T_ASSERT ( dm_bitset_equal ( bs1 , bs1 ) ) ; /* comparing with self */
2011-11-21 01:43:20 +04:00
bit_flip ( bs1 , i ) ;
}
}
2018-04-26 13:59:39 +03:00
static void test_and ( void * fixture )
2011-11-21 01:43:20 +04:00
{
2018-04-26 13:59:39 +03:00
struct dm_pool * mem = fixture ;
2011-11-21 01:43:20 +04:00
dm_bitset_t bs1 = dm_bitset_create ( mem , NR_BITS ) ;
dm_bitset_t bs2 = dm_bitset_create ( mem , NR_BITS ) ;
dm_bitset_t bs3 = dm_bitset_create ( mem , NR_BITS ) ;
int i , j ;
for ( i = 0 , j = 1 ; i < NR_BITS ; i + = j , j + + ) {
dm_bit_set ( bs1 , i ) ;
dm_bit_set ( bs2 , i ) ;
}
dm_bit_and ( bs3 , bs1 , bs2 ) ;
2018-04-26 13:59:39 +03:00
T_ASSERT ( dm_bitset_equal ( bs1 , bs2 ) ) ;
T_ASSERT ( dm_bitset_equal ( bs1 , bs3 ) ) ;
T_ASSERT ( dm_bitset_equal ( bs2 , bs3 ) ) ;
2011-11-21 01:43:20 +04:00
dm_bit_clear_all ( bs1 ) ;
dm_bit_clear_all ( bs2 ) ;
for ( i = 0 ; i < NR_BITS ; i + + ) {
if ( i % 2 )
dm_bit_set ( bs1 , i ) ;
else
dm_bit_set ( bs2 , i ) ;
}
dm_bit_and ( bs3 , bs1 , bs2 ) ;
for ( i = 0 ; i < NR_BITS ; i + + )
2018-04-26 13:59:39 +03:00
T_ASSERT ( ! dm_bit ( bs3 , i ) ) ;
}
# define T(path, desc, fn) register_test(ts, " / base / data-struct / bitset / " path, desc, fn)
void bitset_tests ( struct dm_list * all_tests )
{
struct test_suite * ts = test_suite_create ( _mem_init , _mem_exit ) ;
if ( ! ts ) {
fprintf ( stderr , " out of memory \n " ) ;
exit ( 1 ) ;
}
T ( " get_next " , " get next set bit " , test_get_next ) ;
T ( " equal " , " equality " , test_equal ) ;
T ( " and " , " and all bits " , test_and ) ;
dm_list_add ( all_tests , & ts - > list ) ;
2011-11-21 01:43:20 +04:00
}