Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started work on a framework for writing runtime tests, added some sim…
…ple test cases.
- Loading branch information
Showing
5 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "../rust_internal.h" | ||
|
||
bool | ||
rust_test::run() { | ||
return false; | ||
} | ||
|
||
const char * | ||
rust_test::name() { | ||
return "untitled"; | ||
} | ||
|
||
rust_test_suite::rust_test_suite() { | ||
tests.append(new rust_array_list_test()); | ||
tests.append(new rust_synchronized_indexed_list_test()); | ||
} | ||
|
||
rust_test_suite::~rust_test_suite() { | ||
|
||
} | ||
|
||
bool | ||
rust_test_suite::run() { | ||
bool pass = true; | ||
for (size_t i = 0; i < tests.size(); i++) { | ||
rust_test *test = tests[i]; | ||
printf("test: %s running ... \n", test->name()); | ||
if (tests[i]->run() == false) { | ||
printf("test: %s FAILED\n", test->name()); | ||
pass = false; | ||
} else { | ||
printf("test: %s PASSED\n", test->name()); | ||
} | ||
} | ||
return pass; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef RUST_TEST_HARNESS_H | ||
#define RUST_TEST_HARNESS_H | ||
|
||
#define CHECK(x) if ((x) == false) \ | ||
{ printf("condition: %s failed at file: %s, line: %d\n", #x, \ | ||
__FILE__, __LINE__ ); return false; } | ||
|
||
class rust_test { | ||
public: | ||
virtual bool run(); | ||
virtual const char *name(); | ||
}; | ||
|
||
class rust_test_suite : public rust_test { | ||
public: | ||
array_list<rust_test*> tests; | ||
rust_test_suite(); | ||
virtual ~rust_test_suite(); | ||
bool run(); | ||
}; | ||
|
||
#endif /* RUST_TEST_HARNESS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "../rust_internal.h" | ||
|
||
#define COUNT 1000 | ||
#define LARGE_COUNT 100000 | ||
#define THREADS 10 | ||
|
||
bool | ||
rust_array_list_test::run() { | ||
array_list<int> list; | ||
|
||
for (int i = 0; i < COUNT; i++) { | ||
list.append(i); | ||
} | ||
|
||
for (int i = 0; i < COUNT; i++) { | ||
CHECK (list[i] == i); | ||
} | ||
|
||
for (int i = 0; i < COUNT; i++) { | ||
CHECK (list.index_of(i) == i); | ||
} | ||
|
||
for (int i = 0; i < COUNT; i++) { | ||
CHECK (list.replace(i, -i)); | ||
CHECK (list.replace(-i, i)); | ||
CHECK (list.index_of(i) == i); | ||
} | ||
|
||
for (int i = COUNT - 1; i >= 0; i--) { | ||
CHECK (list.pop(NULL)); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool | ||
rust_synchronized_indexed_list_test::run() { | ||
array_list<worker*> workers; | ||
|
||
for (int i = 0; i < THREADS; i++) { | ||
worker *worker = | ||
new rust_synchronized_indexed_list_test::worker(this); | ||
workers.append(worker); | ||
} | ||
|
||
for (uint32_t i = 0; i < workers.size(); i++) { | ||
workers[i]->start(); | ||
} | ||
|
||
while(workers.is_empty() == false) { | ||
worker *worker; | ||
workers.pop(&worker); | ||
worker->join(); | ||
delete worker; | ||
} | ||
|
||
long long expected_items = LARGE_COUNT * THREADS; | ||
|
||
CHECK(list.length() == expected_items); | ||
|
||
long long sum = 0; | ||
for (size_t i = 0; i < list.length(); i++) { | ||
sum += list[i]->value; | ||
} | ||
|
||
long long expected_sum = LARGE_COUNT; | ||
expected_sum = expected_sum * (expected_sum - 1) / 2 * THREADS; | ||
CHECK (sum == expected_sum); | ||
return true; | ||
} | ||
|
||
void | ||
rust_synchronized_indexed_list_test::worker::run() { | ||
for (int i = 0; i < LARGE_COUNT; i++) { | ||
parent->list.append(new indexed_list_element<int>(i)); | ||
} | ||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef RUST_TEST_UTIL_H | ||
#define RUST_TEST_UTIL_H | ||
|
||
class rust_test_util : public rust_test { | ||
public: | ||
|
||
}; | ||
|
||
class rust_array_list_test : public rust_test { | ||
public: | ||
bool run(); | ||
const char *name() { | ||
return "rust_array_list_test"; | ||
} | ||
}; | ||
|
||
|
||
class rust_synchronized_indexed_list_test : public rust_test { | ||
public: | ||
rust_srv srv; | ||
memory_region region; | ||
synchronized_indexed_list<indexed_list_element<int> > list; | ||
|
||
rust_synchronized_indexed_list_test() : | ||
region(&srv, false), list(®ion) { | ||
// Nop. | ||
} | ||
|
||
class worker : public rust_thread { | ||
public: | ||
rust_synchronized_indexed_list_test *parent; | ||
worker(rust_synchronized_indexed_list_test *parent) : parent(parent) { | ||
// Nop. | ||
} | ||
void run(); | ||
}; | ||
bool run(); | ||
const char *name() { | ||
return "rust_synchronized_indexed_list_test"; | ||
} | ||
}; | ||
|
||
#endif /* RUST_TEST_UTIL_H */ |