aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp.test/src/oo.cpp
blob: ede8a242cf841390d139219326540b1823756839 (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
#include <cstdio>

#include "meowpp/Self.h"
#include <vector>
#include <string>
#include <algorithm>

#include <ctime>
#include <cmath>

using namespace meow;

class A {
private:
  struct Myself{
    int n;
    Myself() { }
    Myself(Myself const& m): n(m.n) {
    }
    ~Myself() { }
  };
  Self<Myself> const self;
public:
  A(): self(){ self()->n = 0; }
  A(A const& b): self(b.self, Self<Myself>::COPY_FROM) { }
  ~A() { }
  int num() const { return self->n; }
  int num(int k) { return (self()->n = k); }
  void copyFrom(A const& v) { self().copyFrom(v.self); }
  void referenceFrom(A const& v) { self().referenceFrom(v.self); }
};

struct B {
  int n;
  int count;
  B() { n = 0; count = 1; }
};

static const size_t N = 50;

static A  as[N];
static B *bs[N];

int main(){
  srand(time(0));
  for (size_t i = 0; i < N; i++) {
    bs[i] = new B;
  }
  for (size_t i = 0; i < 500; i++) {
    int k = rand();
    if (k % 3 == 0) { // copyFrom
      int x, y;
      do {
        x = rand() % N;
        y = rand() % N;
      } while(x == y);
      as[x].copyFrom(as[y]);
      bs[x]->n = bs[y]->n;
    }
    else if (k % 3 == 1) { // referenceFrom
      int x, y;
      do {
        x = rand() % N;
        y = rand() % N;
      } while(x == y || x / (N / 5) != y / (N / 5));
      as[x].referenceFrom(as[y]);
      bs[x]->count--;
      if (bs[x]->count == 0) {
        delete bs[x];
      }
      bs[x] = bs[y];
      bs[x]->count++;
    }
    else { // set value
      int x = rand() % N, v = rand() % 100;
      as[x].num(v);
      bs[x]->n = v;
    }
    bool chk = true;
    for (size_t n = 0; n < N; n++) {
      if (as[n].num() != bs[n]->n) {
        chk = false;
        break;
      }
    }
    if (!chk) {
      printf("false!\n");
      return 1;
    }
    //for (size_t j = 0; j < N; j++) { printf("%d ", as[j].num()); } printf("\n");
  }
  for (size_t i = 0; i < N; i++) { printf("%d ", as[i].num()); }
  printf("\n");
  for (size_t i = 0; i < N; i++) { printf("%d ", bs[i]->n); }
  printf("\n");
  printf("true\n");
  return 0;
}