aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2011-03-20 14:08:23 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2011-03-20 16:20:41 +0000
commit59c03f99c2056d2aa36f54a28146c686349d800a (patch)
tree8b7ccc3c9c8d7e1eab8cb19743cce564b84b1c41
parentfdaa20cdc030d02e0e142c163bf1493af9ca359e (diff)
downloadpaludis-59c03f99c2056d2aa36f54a28146c686349d800a.tar.gz
paludis-59c03f99c2056d2aa36f54a28146c686349d800a.tar.xz
gtest more
-rw-r--r--paludis/util/files.m42
-rw-r--r--paludis/util/singleton_TEST.cc147
2 files changed, 55 insertions, 94 deletions
diff --git a/paludis/util/files.m4 b/paludis/util/files.m4
index cf2ab2c..1c06a80 100644
--- a/paludis/util/files.m4
+++ b/paludis/util/files.m4
@@ -77,7 +77,7 @@ add(`set', `hh', `fwd', `impl', `cc')
add(`sha1', `hh', `cc', `test')
add(`sha256', `hh', `cc', `test')
add(`simple_parser', `hh', `cc', `test', `fwd')
-add(`singleton', `hh', `impl', `test')
+add(`singleton', `hh', `impl', `gtest')
add(`stringify', `hh', `gtest')
add(`string_list_stream', `hh', `cc', `fwd', `gtest')
add(`strip', `hh', `cc', `gtest')
diff --git a/paludis/util/singleton_TEST.cc b/paludis/util/singleton_TEST.cc
index ece4df6..9a43488 100644
--- a/paludis/util/singleton_TEST.cc
+++ b/paludis/util/singleton_TEST.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2005, 2006, 2007, 2008, 2010 Ciaran McCreesh
+ * Copyright (c) 2005, 2006, 2007, 2008, 2010, 2011 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -21,15 +21,14 @@
#include <paludis/util/singleton-impl.hh>
#include <paludis/util/thread.hh>
#include <paludis/util/mutex.hh>
+
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
-#include <test/test_framework.hh>
-#include <test/test_runner.hh>
+#include <gtest/gtest.h>
-using namespace test;
using namespace paludis;
namespace
@@ -113,99 +112,61 @@ namespace
int MyThreadedClass::instances = 0;
Mutex MyThreadedClass::mutex;
+
+ static void thread_func(void * * const p) throw ()
+ {
+ *p = MyThreadedClass::get_instance();
+ }
}
-namespace test_cases
+TEST(Singleton, Works)
{
- struct SingletonPatternTest : TestCase
- {
- SingletonPatternTest() : TestCase("singleton test") { }
-
- bool repeatable() const
- {
- return false;
- }
-
- void run()
- {
- TEST_CHECK_EQUAL(MyClass::instances, 0);
- TEST_CHECK(0 != MyClass::get_instance());
- TEST_CHECK_EQUAL(MyClass::instances, 1);
- TEST_CHECK(MyClass::get_instance() == MyClass::get_instance());
- TEST_CHECK(MyClass::get_instance()->s.empty());
- MyClass::get_instance()->s = "foo";
- TEST_CHECK_EQUAL(MyClass::get_instance()->s, "foo");
- }
- } test_singleton_pattern;
-
- struct SingletonThreadedTest : TestCase
- {
- SingletonThreadedTest() : TestCase("singleton threaded test") { }
-
- bool repeatable() const
- {
- return false;
- }
-
- static void thread_func(void * * const p) throw ()
- {
- *p = MyThreadedClass::get_instance();
- }
-
- void run()
- {
- using namespace std::placeholders;
- const int c = 100;
-
- std::vector<void *> a(c, static_cast<void *>(0));
- TEST_CHECK_EQUAL(MyThreadedClass::instances, 0);
- TEST_CHECK(c == std::count(a.begin(), a.end(), static_cast<void *>(0)));
- {
- std::vector<std::shared_ptr<Thread> > t(c);
- for (int x(0) ; x < c ; ++x)
- t[x] = std::make_shared<Thread>(std::bind(&thread_func, &a[x]));
- }
- TEST_CHECK_EQUAL(MyThreadedClass::instances, 1);
- TEST_CHECK(0 == std::count(a.begin(), a.end(), static_cast<void *>(0)));
- TEST_CHECK(c == std::count(a.begin(), a.end(), MyThreadedClass::get_instance()));
- }
- } test_singleton_threaded;
+ ASSERT_EQ(0, MyClass::instances);
+ ASSERT_TRUE(0 != MyClass::get_instance());
+ ASSERT_EQ(1, MyClass::instances);
+ ASSERT_TRUE(MyClass::get_instance() == MyClass::get_instance());
+ ASSERT_TRUE(MyClass::get_instance()->s.empty());
+ MyClass::get_instance()->s = "foo";
+ ASSERT_EQ("foo", MyClass::get_instance()->s);
+}
- struct SingletonPatternDeleteTest : TestCase
- {
- SingletonPatternDeleteTest() : TestCase("singleton delete test") { }
-
- bool repeatable() const
- {
- return false;
- }
-
- void run()
- {
- TEST_CHECK_EQUAL(MyClassTwo::instances, 0);
- TEST_CHECK(0 != MyClassTwo::get_instance());
- TEST_CHECK_EQUAL(MyClassTwo::instances, 1);
- TEST_CHECK(MyClassTwo::get_instance() == MyClassTwo::get_instance());
- TEST_CHECK(MyClassTwo::get_instance()->s.empty());
- MyClassTwo::get_instance()->s = "foo";
- TEST_CHECK_EQUAL(MyClassTwo::get_instance()->s, "foo");
- MyClassTwo::destroy_instance();
- TEST_CHECK_EQUAL(MyClassTwo::instances, 0);
- TEST_CHECK(0 != MyClassTwo::get_instance());
- TEST_CHECK_EQUAL(MyClassTwo::instances, 1);
- TEST_CHECK(MyClassTwo::get_instance()->s.empty());
- }
- } test_singleton_pattern_delete;
-
- struct SingletonPatternRecursiveTest : TestCase
+TEST(Singleton, Threaded)
+{
+ using namespace std::placeholders;
+ const int c = 100;
+
+ std::vector<void *> a(c, static_cast<void *>(0));
+ ASSERT_EQ(0, MyThreadedClass::instances);
+ ASSERT_TRUE(c == std::count(a.begin(), a.end(), static_cast<void *>(0)));
{
- SingletonPatternRecursiveTest() : TestCase("singleton recursive test") { }
-
- void run()
- {
- TEST_CHECK_THROWS(MyRecursiveClass * PALUDIS_ATTRIBUTE((unused)) x =
- MyRecursiveClass::get_instance(), InternalError);
- }
- } test_singleton_pattern_recurse;
+ std::vector<std::shared_ptr<Thread> > t(c);
+ for (int x(0) ; x < c ; ++x)
+ t[x] = std::make_shared<Thread>(std::bind(&thread_func, &a[x]));
+ }
+ ASSERT_EQ(1, MyThreadedClass::instances);
+ ASSERT_TRUE(0 == std::count(a.begin(), a.end(), static_cast<void *>(0)));
+ ASSERT_TRUE(c == std::count(a.begin(), a.end(), MyThreadedClass::get_instance()));
+}
+
+TEST(Singleton, Delete)
+{
+ ASSERT_EQ(0, MyClassTwo::instances);
+ ASSERT_TRUE(0 != MyClassTwo::get_instance());
+ ASSERT_EQ(1, MyClassTwo::instances);
+ ASSERT_TRUE(MyClassTwo::get_instance() == MyClassTwo::get_instance());
+ ASSERT_TRUE(MyClassTwo::get_instance()->s.empty());
+ MyClassTwo::get_instance()->s = "foo";
+ ASSERT_EQ("foo", MyClassTwo::get_instance()->s);
+ MyClassTwo::destroy_instance();
+ ASSERT_EQ(MyClassTwo::instances, 0);
+ ASSERT_TRUE(0 != MyClassTwo::get_instance());
+ ASSERT_EQ(1, MyClassTwo::instances);
+ ASSERT_TRUE(MyClassTwo::get_instance()->s.empty());
+}
+
+TEST(Singleton, Recursive)
+{
+ ASSERT_THROW(MyRecursiveClass * PALUDIS_ATTRIBUTE((unused)) x =
+ MyRecursiveClass::get_instance(), InternalError);
}