Add boolean_vector classes
[libstick.git] / tests / booleanvector.h
diff --git a/tests/booleanvector.h b/tests/booleanvector.h
new file mode 100644 (file)
index 0000000..ebf6b9f
--- /dev/null
@@ -0,0 +1,99 @@
+#ifndef booleanvector_h_MeiTephaijaequuy
+#define booleanvector_h_MeiTephaijaequuy
+
+#include <cpptest.h>
+#include <cpptest-suite.h>
+
+#include <libstick-0.1/booleanvector.h>
+
+using namespace libstick;
+
+
+class boolean_vector_TestSuite: public Test::Suite {
+    public:
+        boolean_vector_TestSuite() {
+            TEST_ADD(boolean_vector_TestSuite::test<sorted_boolean_vector<int> >);
+            TEST_ADD(boolean_vector_TestSuite::test<heapsorted_boolean_vector<int> >);
+
+            //TEST_ADD(boolean_vector_TestSuite::test_performance<sorted_boolean_vector<int> >);
+            //TEST_ADD(boolean_vector_TestSuite::test_performance<heapsorted_boolean_vector<int> >);
+        }
+
+    protected:
+        virtual void setup() {
+        }
+
+        virtual void tear_down() {
+        }
+
+    private:
+        template<typename T>
+        void test() {
+            const int size = 1000;
+
+            T even;
+            for (int i=1; 2*i <= size; ++i)
+                even.set(2*i, true);
+
+            // We added size/2 ones, all of them at even indices.
+            TEST_ASSERT(even.get_ones().size() == size/2);
+            for (typename T::indexarray::const_iterator it = even.get_ones().begin();
+                    it != even.get_ones().end(); ++it)
+                TEST_ASSERT(*it % 2 == 0);
+            for (int i=1; i <= size; ++i)
+                TEST_ASSERT(even.get(i) == (i % 2 == 0));
+
+            // A complete vector
+            T all;
+            for (int i=1; i <= size; ++i)
+                all.set(i, true);
+
+            // A vector with all odd entries
+            T odd = even;
+            odd.add(all);
+
+            // Again size/2 entries in odd.
+            TEST_ASSERT(odd.get_ones().size() == size/2);
+            // Check whether we really have odd indices only
+            for (typename T::indexarray::const_iterator it = odd.get_ones().begin();
+                    it != odd.get_ones().end(); ++it)
+                TEST_ASSERT(*it % 2 == 1);
+            for (int i=1; i <= size; ++i)
+                TEST_ASSERT(odd.get(i) == (i % 2 == 1));
+
+
+            all.pop_back();
+            even.pop_back();
+            T all2 = odd;
+            all2.add(even);
+            TEST_ASSERT(all2 == all);
+        }
+
+        template<typename T>
+        void test_performance() {
+            T sum;
+
+            const int size = 1000;
+            const int cnt = 4;
+
+            T summand;
+            for (int a=0; a < cnt; ++a) {
+                summand.clear();
+                for (int i=0; i < size; ++i)
+                    summand.set(a*(size/2) + i, true);
+                sum.add(summand);
+            }
+
+            //for (int i=0; i < size/2; ++i) {
+                //TEST_ASSERT(sum.get(i) == true);
+                //TEST_ASSERT(sum.get(i + size/2) == false);
+                //TEST_ASSERT(sum.get(i + (cnt-1)*(size/2)) == false);
+                //TEST_ASSERT(sum.get(i + (cnt)*(size/2)) == true);
+            //}
+
+            //std::cout << std::endl << sum << std::endl;
+        }
+};
+
+
+#endif