feisty meow concerns codebase 2.140
test_bit_vector.cpp
Go to the documentation of this file.
1/*****************************************************************************\
2* *
3* Name : test_bit_vector *
4* Author : Chris Koeritz *
5* *
6*******************************************************************************
7* Copyright (c) 1991-$now By Author. This program is free software; you can *
8* redistribute it and/or modify it under the terms of the GNU General Public *
9* License as published by the Free Software Foundation; either version 2 of *
10* the License or (at your option) any later version. This is online at: *
11* http://www.fsf.org/copyleft/gpl.html *
12* Please send any updates to: fred@gruntose.com *
13\*****************************************************************************/
14
16#include <basis/array.h>
17#include <basis/astring.h>
18#include <basis/byte_array.h>
19#include <basis/functions.h>
20#include <basis/guards.h>
22#include <mathematics/chaos.h>
25#include <unit_test/unit_base.h>
26
27#include <memory.h>
28#include <stdlib.h>
29
30using namespace application;
31using namespace basis;
32using namespace filesystem;
33using namespace loggers;
34using namespace mathematics;
35using namespace structures;
36using namespace textual;
37using namespace timely;
38using namespace unit_test;
39
40#define LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), to_print)
41
42#define MAX_TEST 100
43#define FOOP_MAX 213
44
46
47class test_bit_vector : virtual public unit_base, virtual public application_shell
48{
49public:
50 test_bit_vector() : unit_base() {}
51 DEFINE_CLASS_NAME("test_bit_vector");
52 virtual int execute();
53};
54
55HOOPLE_MAIN(test_bit_vector, );
56
58
59struct test_struct { basis::un_int store; int posn; int size; };
60
61int test_bit_vector::execute()
62{
63 FUNCDEF("execute");
65
66 const array<test_struct> unused;
67
69 bit_vector foop(FOOP_MAX);
70
71 for (int i = 0; i < MAX_TEST; i++) {
72 // sets a random bit and finds that one.
74 foop.light(rando);
75 int found = foop.find_first(true);
76 ASSERT_EQUAL(found, rando, "find first locates first true");
77 foop.clear(rando);
78
79 foop.resize(FOOP_MAX);
80 ASSERT_EQUAL(0, foop.find_first(0), "locating location of first zero");
81 ASSERT_EQUAL(common::NOT_FOUND, foop.find_first(1), "showing there are no one bits");
82 for (int i = 0; i < 12; i++) foop.light(i);
83 ASSERT_EQUAL(12, foop.find_first(0), "finding first on partially set vector");
84
85 foop.light(FOOP_MAX); // shouldn't work, but shouldn't die.
86 ASSERT_FALSE(foop.on(FOOP_MAX), "bit_on should not be lit past end of vector");
87
88 // sets a bunch of random bits.
89 for (int j = 0; j < 40; j++) {
91 foop.light(rando);
92 }
93 bit_vector foop2(FOOP_MAX, ((const byte_array &)foop).observe());
94 ASSERT_EQUAL(foop, foop2, "after lighting, vectors should be identical");
95
96 {
97 // this block tests the subvector and int storage/retrieval routines.
98 if (foop.bits() < 90) foop.resize(90); // make sure we have room to play.
99
100 array<test_struct> tests;
101 test_struct t1 = { 27, 15, 5 };
102 tests += t1;
103 test_struct t2 = { 8, 25, 4 };
104 tests += t2;
105 test_struct t3 = { 1485, 34, 16 };
106 tests += t3;
107 test_struct t4 = { 872465, 50, 32 };
108 tests += t4;
109
110 for (int i = 0; i < tests.length(); i++) {
111 ASSERT_TRUE(foop.set(tests[i].posn, tests[i].size, tests[i].store),
112 "storing int in vector should work");
113
114//hmmm: make this a test case!
115// bit_vector found = foop.subvector(tests[i].posn, tests[i].posn+tests[i].size-1);
116// LOG(astring(astring::SPRINTF, "contents found:\n%s", found.text_form().s()));
117
118 basis::un_int to_check = foop.get(tests[i].posn, tests[i].size);
119 if (to_check != tests[i].store)
120 LOG(a_sprintf("int found at %d in vector (%u) is different than what was stored (%u).",
121 i, to_check, tests[i].store));
122 ASSERT_EQUAL((int)to_check, (int)tests[i].store, "should see expected int stored in vector");
123 }
124 }
125
126 {
127 // tests random resizings and resettings.
128 int number_of_loops = randomizer.inclusive(50, 150);
129 for (int i = 0; i < number_of_loops; i++) {
130 int which_to_do = randomizer.inclusive(1, 3);
131 switch (which_to_do) {
132 case 1: {
133 // resize.
134 int new_size = randomizer.inclusive(0, 32000);
135 foop.resize(new_size);
136 break;
137 }
138 case 2: {
139 // reset.
140 int new_size = randomizer.inclusive(0, 32000);
141 foop.reset(new_size);
142 break;
143 }
144 case 3: {
145 // random sets.
146 int sets_to_do = randomizer.inclusive(40, 280);
147 for (int i = 0; i < sets_to_do; i++) {
148 int rando = randomizer.inclusive(0, foop.bits());
149 if (randomizer.inclusive(0, 1)) foop.light(rando);
150 else foop.clear(rando);
151 }
152 break;
153 }
154 }
155 }
156 }
157
158 foop.reset(FOOP_MAX); // to clear before next loop.
159 }
160
161 return final_report();
162}
163
The application_shell is a base object for console programs.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
a_sprintf is a specialization of astring that provides printf style support.
Definition astring.h:440
Represents a sequential, ordered, contiguous collection of objects.
Definition array.h:54
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
Definition array.h:349
const contents & get(int index) const
Accesses individual objects stored in "this" at the "index" position.
Definition array.h:372
outcome resize(int new_size, how_to_copy way=NEW_AT_END)
Changes the size of the C array to "new_size".
Definition array.h:585
int length() const
Returns the current reported length of the allocated C array.
Definition array.h:115
A very common template for a dynamic array of bytes.
Definition byte_array.h:36
a platform-independent way to acquire random numbers in a specific range.
Definition chaos.h:51
int inclusive(int low, int high) const
< Returns a pseudo-random number r, such that "low" <= r <= "high".
Definition chaos.h:88
An array of bits with operations for manipulating and querying individual bits.
Definition bit_vector.h:26
#define SETUP_COMBO_LOGGER
a macro that retasks the program-wide logger as a combo_logger.
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition hoople_main.h:61
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
unsigned int un_int
Abbreviated name for unsigned integers.
Definition definitions.h:62
A platform independent way to obtain the timestamp of a file.
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition averager.h:21
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
#include <time.h>
Useful support functions for unit testing, especially within hoople.
Definition unit_base.cpp:35
#define randomizer()
#define FOOP_MAX
#define MAX_TEST
#define LOG(to_print)
chaos rando
#define ASSERT_EQUAL(a, b, test_name)
Definition unit_base.h:38
#define ASSERT_TRUE(a, test_name)
Definition unit_base.h:46
#define ASSERT_FALSE(a, test_name)
Definition unit_base.h:50