aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/utility/self_test.cpp
blob: 4756f0f0b849cd9773f4abe4fda7d0b20bf689f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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;
}