File: SyncTest.cpp

package info (click to toggle)
storm-lang 0.7.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,100 kB
  • sloc: ansic: 261,471; cpp: 140,438; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (95 lines) | stat: -rw-r--r-- 1,536 bytes parent folder | download | duplicates (2)
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
#include "stdafx.h"

struct OtherData {
	os::Sema sync;
	os::Sema ready;
	Nat result;

	OtherData() : sync(0), ready(0), result(0) {}

	void run() {
		sync.down();
		Sleep(100);
		result = 201;
		ready.up();
	}
};

BEGIN_TEST(SyncTest, OS) {
	os::ThreadGroup group;
	OtherData data;

	os::Thread::spawn(util::memberVoidFn(&data, &OtherData::run), group);
	Sleep(100);
	data.sync.up();
	data.ready.down();
	CHECK_EQ(data.result, 201);

	group.join();
} END_TEST

class InlineOrder {
public:
	InlineOrder(int v) : v(v), next(null), prev(null) {}
	InlineOrder *next;
	InlineOrder *prev;

	int v;

	inline bool operator <(const InlineOrder &o) const {
		return v < o.v;
	}
};

BEGIN_TEST(WaitTest, OS) {
	os::SortedInlineList<InlineOrder> l;

	InlineOrder a(10);
	InlineOrder b(20);
	InlineOrder c(30);
	InlineOrder d(40);

	l.push(&b);
	l.push(&d);
	l.push(&a);
	l.push(&c);

	CHECK_EQ(l.pop()->v, 10);
	CHECK_EQ(l.pop()->v, 20);
	CHECK_EQ(l.pop()->v, 30);
	CHECK_EQ(l.pop()->v, 40);
	CHECK(l.pop() == null);

	os::UThread::sleep(100);
} END_TEST

class OtherCond {
public:
	os::IOCondition cond;
	nat running;

	OtherCond() : running(1) {}

	void run() {
		for (nat i = 0; i < 10000; i++) {
			cond.wait();
		}

		running = 0;
	}
};

/**
 * Make sure the IOCondition does not hang under heavy load.
 */
BEGIN_TEST(IOConditionTest, OS) {
	os::ThreadGroup group;
	OtherCond data;
	os::Thread::spawn(util::memberVoidFn(&data, &OtherCond::run), group);

	while (atomicRead(data.running)) {
		data.cond.signal();
	}

	group.join();
} END_TEST