File: ArrayTest.cpp

package info (click to toggle)
storm-lang 0.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 51,836 kB
  • sloc: ansic: 261,420; cpp: 138,870; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (151 lines) | stat: -rw-r--r-- 3,154 bytes parent folder | download | duplicates (4)
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
#include "stdafx.h"
#include "Core/Array.h"
#include "Core/Str.h"

#include "Core/Random.h"
#include "Core/Timing.h"

BEGIN_TEST(ArrayTest, Core) {
	Engine &e = gEngine();

	Array<Str *> *t = new (e) Array<Str *>();
	CHECK_EQ(toS(t), L"[]");

	t->push(new (e) Str(L"Hello"));
	t->push(new (e) Str(L"World"));
	CHECK_EQ(toS(t), L"[Hello, World]");

	t->insert(0, new (e) Str(L"Well"));
	CHECK_EQ(toS(t), L"[Well, Hello, World]");

	t->append(t);
	CHECK_EQ(toS(t), L"[Well, Hello, World, Well, Hello, World]");

} END_TEST

BEGIN_TEST(ArrayExTest, CoreEx) {
	Engine &e = gEngine();

	// Check automatically generated toS functions.

	Array<Value> *v = new (e) Array<Value>();
	CHECK_EQ(toS(v), L"[]");

	v->push(Value(Str::stormType(e)));
	CHECK_EQ(toS(v), L"[core.Str]");

} END_TEST

static bool CODECALL predicate(Int a, Int b) {
	a = (a + 5) % 10;
	b = (b + 5) % 10;
	return a < b;
}

BEGIN_TEST(ArraySortTest, CoreEx) {
	Engine &e = gEngine();

	Array<Int> *v = new (e) Array<Int>();
	for (Int i = 0; i < 10; i++)
		*v << (10 - i);

	v->sort();
	CHECK_EQ(toS(v), L"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]");

	// Try with a predicate!
	v->sort(fnPtr(e, &predicate));
	CHECK_EQ(toS(v), L"[5, 6, 7, 8, 9, 10, 1, 2, 3, 4]");

	// See if 'sorted' works as expected.
	CHECK_EQ(toS(v->sorted()), L"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]");
	CHECK_EQ(toS(v), L"[5, 6, 7, 8, 9, 10, 1, 2, 3, 4]");
} END_TEST

BEGIN_TEST(ArrayRemoveDupTest, CoreEx) {
	Engine &e = gEngine();

	Array<Int> *v = new (e) Array<Int>();
	v->push(1);
	v->push(2);
	v->push(2);
	v->push(3);
	v->push(3);
	v->push(3);
	v->push(4);

	Array<Int> *w = v->withoutDuplicates();
	CHECK_EQ(toS(w), L"[1, 2, 3, 4]");
	CHECK_EQ(toS(v), L"[1, 2, 2, 3, 3, 3, 4]");

	v->removeDuplicates();
	CHECK_EQ(toS(v), L"[1, 2, 3, 4]");
} END_TEST

BEGIN_TEST(ArrayBinSearchTest, CoreEx) {
	Engine &e = gEngine();

	Array<Int> *v = new (e) Array<Int>();
	v->push(1);
	v->push(2);
	v->push(2);
	v->push(3);
	v->push(3);
	v->push(3);
	v->push(4);
	v->push(4);

	CHECK_EQ(v->lowerBound(3), 3);
	CHECK_EQ(v->upperBound(3), 6);
	CHECK_EQ(v->lowerBound(0), 0);
	CHECK_EQ(v->upperBound(0), 0);
	CHECK_EQ(v->lowerBound(4), 6);
	CHECK_EQ(v->upperBound(4), 8);
	CHECK_EQ(v->lowerBound(10), 8);
	CHECK_EQ(v->upperBound(10), 8);
} END_TEST

BEGIN_TEST(ArrayCompareTest, CoreEx) {
	Engine &e = gEngine();

	Array<Int> *a = new (e) Array<Int>();
	a->push(10);
	a->push(20);
	a->push(30);

	Array<Int> *b = new (e) Array<Int>();
	b->push(10);
	b->push(30);

	Array<Int> *c = new (e) Array<Int>();
	c->push(10);
	c->push(30);

	CHECK(a->lessRaw(b));
	CHECK(!b->lessRaw(a));

	CHECK(!a->equalRaw(b));
	CHECK(b->equalRaw(c));
} END_TEST

// Performance of sort()
BEGIN_TESTX(ArraySortPerf, CoreEx) {
	Engine &e = gEngine();

	Array<Int> *v = new (e) Array<Int>();
	vector<Int> s;
	for (Int i = 0; i < 100000; i++) {
		Int val = rand(0, 1000000);
		*v << val;
		s.push_back(val);
	}

	Moment start;
	v->sort();
	Moment half;
	std::sort(s.begin(), s.end());
	Moment end;

	PLN(L"Sorted " << v->count() << L" integers:");
	PLN(L"  " << (half - start) << L" using storm::sort");
	PLN(L"  " << (end - half)   << L" using std::sort");
} END_TEST