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

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

#include <ctime>

using namespace meow;

class A {
private:
  struct Myself{
    int n;
    Myself() { }
    ~Myself() { }
    void copyFrom(Myself const& m){ n = m.n; }
  };
  Self<Myself> const self;
public:
  A(): self(true){ self()->n = 0; }
  A(A const& b): self(false) { copyFrom(b); }
  ~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 A as[100];
static B *bs[100];

static size_t N = 100;


int main(){
  for (size_t i = 0; i < N; i++) {
    bs[i] = new B;
  }
  for (size_t i = 0; i < 1000; i++) {
    int k = rand();
    if (k % 3 == 0) {
      int x, y;
      do {
        x = rand() % N;
        y = rand() % N;
      } while(x == y);
      as[x].copyFrom(as[y]);
      bs[x]->count--;
      if (bs[x]->count == 0) {
        delete bs[x];
      }
      bs[x] = new B;
      bs[x]->n = bs[y]->n;
    }
    else if (k % 3 == 1) {
      int x, y;
      do {
        x = rand() % N;
        y = rand() % N;
      } while(x == y);
      as[x].referenceFrom(as[y]);
      bs[x]->count--;
      if (bs[x]->count == 0) {
        delete bs[x];
      }
      bs[x] = bs[y];
      bs[x]->count++;
    }
    else {
      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 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;
}