diff options
author | cathook <b01902109@csie.ntu.edu.tw> | 2014-09-24 13:37:42 +0800 |
---|---|---|
committer | cathook <b01902109@csie.ntu.edu.tw> | 2014-09-29 16:55:57 +0800 |
commit | 8b76fbb408f8eedab24195655c45c891af01eaab (patch) | |
tree | 414d7fc87885cb77e181a3ab99e334b837621036 /meowpp_unittest/utility/self.cpp | |
parent | ef9af0d577c3a6b5d11fdeed7a9149d09973171b (diff) | |
download | meow-8b76fbb408f8eedab24195655c45c891af01eaab.tar.gz meow-8b76fbb408f8eedab24195655c45c891af01eaab.tar.zst meow-8b76fbb408f8eedab24195655c45c891af01eaab.zip |
Big change, detail see README.
Diffstat (limited to 'meowpp_unittest/utility/self.cpp')
-rw-r--r-- | meowpp_unittest/utility/self.cpp | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/meowpp_unittest/utility/self.cpp b/meowpp_unittest/utility/self.cpp new file mode 100644 index 0000000..4756f0f --- /dev/null +++ b/meowpp_unittest/utility/self.cpp @@ -0,0 +1,188 @@ +#define MEOWPP_UTILITY_SELF_TESTING + +#include <meowpp/debug/assert.h> +#include <meowpp/debug/assert.h> +#include <meowpp/debug/assert.h> +#include <meowpp/utility/self.h> + +#include <cstdlib> + +static int counter = 0; + +class A { + private: + struct DataMember { + int var1; + int var2; + int counter; + DataMember() : var1(0), var2(0), counter(0) { + ++counter; + } + DataMember(DataMember const& b) : + var1(b.var1), var2(b.var2), counter(0) { + ++counter; + } + DataMember(int var1_init_value) : + var1(var1_init_value), var2(0), counter(0) { + ++counter; + } + + ~DataMember() { + --counter; + } + + DataMember const& CopyFrom(DataMember const& b) { + var1 = b.var1; + var2 = b.var2; + return *this; + } + }; + + meow::Self<DataMember> const self_; + + public: + A() {} + A(A const& another_class_a) : self_(another_class_a.self_) {} + A(int var1_init_value) : self_(DataMember(var1_init_value)) {} + ~A() {} + + int GetVar1() const { + return self_->var1; + } + + void SetVar1(int new_value) { + int old_value = self_->var1; + self_()->var1 = new_value; + if (old_value != new_value) { + self_()->var2 = old_value; + } + } + + int GetVar2() const { + self_()->counter += 1; + return self_->var2; + } + + bool operator==(A const& b) const { + return (self_->var1 == b.self_->var1 && self_->var2 == b.self_->var2); + } + + bool Is(A const& b) const { + return (self_.Is(b.self_)); + } +}; + +namespace meow { +class SelfTest { + private: + struct Data { + int a, b, *c; + Data() : a(1), b(2), c(NULL) {} + Data(int aa, int bb, int* cc) : a(aa), b(bb), c(cc) {} + ~Data() { + if (c) ++(*c); + } + Data& CopyFrom(Data const& x) { + if (c) (*c) += 100; + a = x.a; + b = x.b; + c = x.c; + return *this; + } + bool operator==(Data const& x) const { + return (a == x.a && b == x.b && c == x.c); + } + bool operator!=(Data const& x) const { + return (!(*this == x)); + } + }; + public: + bool test() { + int remove; + { + Self<Data> s1, s2(Data(2, 3, &remove)); + Assert(s1.body_ != NULL, ""); + Assert(s1.body_->counter == 1, ""); + Assert(s1.body_->body == Data(), ""); + Assert(s2.body_ != NULL, ""); + Assert(s2.body_->counter == 1, ""); + Assert(s2.body_->body != Data(), ""); + Assert(s2.body_->body == Data(2, 3, &remove), ""); + Self<Data> s3(s1); + Assert(s3.body_ == s1.body_, ""); + Assert(s3.body_->counter == 2, ""); + Assert(s3.body_->body == Data(), ""); + Self<Data> s4(s2.Copy()); + Assert(s2.body_ != NULL, ""); + Assert(s2.body_->counter == 1, ""); + Assert(s2.body_->body != Data(), ""); + Assert(s2.body_->body == Data(2, 3, &remove), ""); + Assert(s2.body_ != s4.body_, ""); + Assert(s4.body_ != NULL, ""); + Assert(s4.body_->counter == 1, ""); + Assert(s4.body_->body != Data(), ""); + Assert(s4.body_->body == Data(2, 3, &remove), ""); + Assert(s4.body_->body.c == &remove, ""); + Assert(s3.Is(s1) && s1.Is(s3), ""); + Assert((!s2.Is(s4)) && (!s4.Is(s2)), ""); + Self<Data> s5(s2); + remove = 0; + s2.ReferenceFrom(s1); + Assert(s2.body_ != s5.body_, ""); + Assert(s2.Is(s1), ""); + Assert(remove == 0, ""); + Assert(s2.body_ == s1.body_, ""); + Assert(s3.body_->counter == 3, ""); + Assert(s3.body_->body == Data(), ""); + remove = 0; + Assert(s4.body_->body.c == &remove, ""); + s4.CopyFrom(s1); + Assert(s4.body_->body.c != &remove, ""); + Assert(remove == 100, "remove = %d\n", remove); + Assert(s4.body_ != s1.body_, ""); + Assert(s4.body_->counter == 1, ""); + Assert(s4.body_->body == s1.body_->body, ""); + Assert(!(s4.Is(s1)), ""); + s4.Attach(s4.body_); + Assert(s4.body_->counter == 2, ""); + s4.Attach(s4.body_); + Assert(s4.body_->counter == 3, ""); + s4.Detach(); + s4.Detach(); + Assert(s4.body_->counter == 1, ""); + remove = 0; + printf("%llu %d\n", (unsigned long long)s1.body_, s1.body_->counter); + printf("%llu %d\n", (unsigned long long)s2.body_, s2.body_->counter); + printf("%llu %d\n", (unsigned long long)s3.body_, s3.body_->counter); + printf("%llu %d\n", (unsigned long long)s4.body_, s4.body_->counter); + printf("%llu %d\n", (unsigned long long)s5.body_, s5.body_->counter); + s1.body_->body.c = &remove; + s4.body_->body.c = &remove; + s5.body_->body.c = &remove; + } + Assert(remove == 3, "remove = %d\n", remove); + return true; + } +}; +} + +int main() { + bool ok = true; + { + A a1; + A a2(a1); + a2.SetVar1(123); + if (!(a1 == a2)) { + ok = false; + } + A a3(32); + } + if (counter != 0) { + ok = false; + } + if (ok) { + meow::SelfTest tester; + tester.test(); + } + return ok ? 0 : 1; +} |