aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp.test/src/oo.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'meowpp.test/src/oo.cpp')
-rw-r--r--meowpp.test/src/oo.cpp32
1 files changed, 15 insertions, 17 deletions
diff --git a/meowpp.test/src/oo.cpp b/meowpp.test/src/oo.cpp
index 54c69d5..ede8a24 100644
--- a/meowpp.test/src/oo.cpp
+++ b/meowpp.test/src/oo.cpp
@@ -6,6 +6,7 @@
#include <algorithm>
#include <ctime>
+#include <cmath>
using namespace meow;
@@ -14,13 +15,14 @@ private:
struct Myself{
int n;
Myself() { }
+ Myself(Myself const& m): n(m.n) {
+ }
~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(): 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); }
@@ -34,38 +36,33 @@ struct B {
B() { n = 0; count = 1; }
};
-static A as[100];
-static B *bs[100];
-
-static size_t N = 100;
+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 < 1000; i++) {
+ for (size_t i = 0; i < 500; i++) {
int k = rand();
- if (k % 3 == 0) {
+ 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]->count--;
- if (bs[x]->count == 0) {
- delete bs[x];
- }
- bs[x] = new B;
bs[x]->n = bs[y]->n;
}
- else if (k % 3 == 1) {
+ else if (k % 3 == 1) { // referenceFrom
int x, y;
do {
x = rand() % N;
y = rand() % N;
- } while(x == y);
+ } while(x == y || x / (N / 5) != y / (N / 5));
as[x].referenceFrom(as[y]);
bs[x]->count--;
if (bs[x]->count == 0) {
@@ -74,7 +71,7 @@ int main(){
bs[x] = bs[y];
bs[x]->count++;
}
- else {
+ else { // set value
int x = rand() % N, v = rand() % 100;
as[x].num(v);
bs[x]->n = v;
@@ -90,6 +87,7 @@ int main(){
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");