Skip to content

Commit 500d0be

Browse files
committed
added some basic tests
1 parent 10ba906 commit 500d0be

File tree

12 files changed

+495
-0
lines changed

12 files changed

+495
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"transfer_files": [
3+
"bin/DEMO.8xp"
4+
],
5+
"target": {
6+
"name": "DEMO",
7+
"isASM": true
8+
},
9+
"sequence": [
10+
"action|launch",
11+
"delay|200",
12+
"hashWait|1",
13+
"key|enter",
14+
"delay|300",
15+
"hashWait|2"
16+
],
17+
"hashes": {
18+
"1": {
19+
"description": "All tests passed",
20+
"timeout": 5000,
21+
"start": "vram_start",
22+
"size": "vram_16_size",
23+
"expected_CRCs": [
24+
"38E2AD5A"
25+
]
26+
},
27+
"2": {
28+
"description": "Exit",
29+
"start": "vram_start",
30+
"size": "vram_16_size",
31+
"expected_CRCs": [
32+
"FFAF89BA",
33+
"101734A5",
34+
"9DA19F44",
35+
"A32840C8",
36+
"349F4775"
37+
]
38+
}
39+
}
40+
}

test/standalone/cxx_stl/makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = DEMO
6+
ICON = icon.png
7+
DESCRIPTION = "CE C Toolchain Demo"
8+
COMPRESSED = NO
9+
ARCHIVED = NO
10+
11+
CFLAGS = -Wall -Wextra -Wno-deprecated-declarations -std=c17 -Oz
12+
CXXFLAGS = -Wall -Wextra -Wno-deprecated-declarations -std=c++20 -Oz
13+
14+
PREFER_OS_LIBC = NO
15+
16+
# ----------------------------
17+
18+
include $(shell cedev-config --makefile)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef COMMON_H
2+
#define COMMON_H
3+
4+
#include "fake_iostream.h"
5+
6+
using namespace fake;
7+
8+
#define C(expr) if (!(expr)) { return __LINE__; }
9+
10+
#endif /* COMMON_H */
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#ifndef FAKE_IOSTREAM_H
2+
#define FAKE_IOSTREAM_H
3+
4+
#include <cstdio>
5+
#include <string>
6+
#include <string_view>
7+
8+
namespace fake {
9+
10+
struct ostream {};
11+
12+
static ostream cout;
13+
14+
inline ostream& operator<<(ostream& os, char c) {
15+
fputc(c, stdout);
16+
return os;
17+
}
18+
19+
inline ostream& operator<<(ostream& os, const char* x) {
20+
printf("%s", x);
21+
return os;
22+
}
23+
24+
inline ostream& operator<<(ostream& os, const std::string& x) {
25+
printf("%s", x.c_str());
26+
return os;
27+
}
28+
29+
inline ostream& operator<<(ostream& os, const std::string_view& x) {
30+
fwrite(x.data(), sizeof(char), x.size(), stdout);
31+
return os;
32+
}
33+
34+
inline ostream& operator<<(ostream& os, bool b) {
35+
#if 0
36+
fputc(b ? '1' : '0', stdout);
37+
#else
38+
os << (b ? "true" : "false");
39+
#endif
40+
return os;
41+
}
42+
43+
inline ostream& operator<<(ostream& os, void const * x) {
44+
printf("%p", x);
45+
return os;
46+
}
47+
48+
inline ostream& operator<<(ostream& os, void const volatile * x) {
49+
printf("%p", (void const*)x);
50+
return os;
51+
}
52+
53+
inline ostream& operator<<(ostream& os, nullptr_t) {
54+
printf("%p", nullptr);
55+
return os;
56+
}
57+
58+
inline ostream& operator<<(ostream& os, int x) {
59+
printf("%d", x);
60+
return os;
61+
}
62+
63+
inline ostream& operator<<(ostream& os, unsigned int x) {
64+
printf("%u", x);
65+
return os;
66+
}
67+
68+
inline ostream& operator<<(ostream& os, float x) {
69+
printf("%f", x);
70+
return os;
71+
}
72+
73+
inline ostream& operator<<(ostream& os, double x) {
74+
printf("%f", x);
75+
return os;
76+
}
77+
78+
constexpr char endl = '\n';
79+
constexpr char ends = '\0';
80+
81+
constexpr char dec = '\0';
82+
constexpr char oct = '\0';
83+
constexpr char hex = '\0';
84+
constexpr char basefield = '\0';
85+
constexpr char left = '\0';
86+
constexpr char right = '\0';
87+
constexpr char internal = '\0';
88+
constexpr char adjustfield = '\0';
89+
constexpr char scientific = '\0';
90+
constexpr char fixed = '\0';
91+
constexpr char floatfield = '\0';
92+
constexpr char boolalpha = '\0';
93+
constexpr char showbase = '\0';
94+
constexpr char showpoint = '\0';
95+
constexpr char showpos = '\0';
96+
constexpr char skipws = '\0';
97+
constexpr char unitbuf = '\0';
98+
constexpr char uppercase = '\0';
99+
100+
} // namespace fake
101+
102+
#endif /* FAKE_IOSTREAM_H */
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <ti/screen.h>
2+
#include <ti/getcsc.h>
3+
#include <sys/util.h>
4+
#include <ti/sprintf.h>
5+
#include <cstdio>
6+
7+
#define TEST(test) { ret = test; if (ret != 0) { return ret; }}
8+
9+
int test_basic();
10+
int test_apply();
11+
int test_tuple();
12+
int test_span();
13+
int test_charconv();
14+
int test_random();
15+
16+
extern "C" void outchar(char ch) {
17+
char *ptr = (char*)0xFB0000;
18+
*ptr++ = ch;
19+
*ptr++ = '\0';
20+
}
21+
22+
23+
extern "C" void ti_outchar(char ch);
24+
25+
void ti_puts(const char* str) {
26+
while(*str != '\0') {
27+
ti_outchar(*str);
28+
str++;
29+
}
30+
}
31+
32+
int run_tests(void) {
33+
int ret = 0;
34+
35+
TEST(test_basic());
36+
TEST(test_apply());
37+
TEST(test_tuple());
38+
TEST(test_span());
39+
TEST(test_charconv());
40+
TEST(test_random());
41+
42+
return ret;
43+
}
44+
45+
int main(void) {
46+
os_ClrHome();
47+
int failed_test = run_tests();
48+
if (failed_test != 0) {
49+
char buf[sizeof("Failed test L-8388608\n")];
50+
boot_sprintf(buf, "Failed test L%d\n", failed_test);
51+
ti_puts(buf);
52+
} else {
53+
ti_puts("All tests passed");
54+
}
55+
56+
while (!os_GetCSC());
57+
58+
return 0;
59+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _ti_outchar
6+
7+
_ti_outchar:
8+
pop de
9+
ex (sp), hl
10+
push de
11+
ld iy, $d00080 ; ti.flags
12+
ld a, l
13+
cp a, 32
14+
jp nc, $207B8 ; ti.PutC
15+
cp a, 10
16+
jp z, $0207F0 ; ti.NewLine
17+
ret
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "common.h"
2+
3+
static int add(int first, int second) { return first + second; }
4+
5+
template<typename T>
6+
T add_generic(T first, T second) { return first + second; }
7+
8+
auto add_lambda = [](auto first, auto second) { return first + second; };
9+
10+
template<typename... Ts>
11+
ostream& operator<<(ostream& os, std::tuple<Ts...> const& theTuple)
12+
{
13+
std::apply
14+
(
15+
[&os](Ts const&... tupleArgs)
16+
{
17+
os << '[';
18+
std::size_t n{0};
19+
((os << tupleArgs << (++n != sizeof...(Ts) ? ", " : "")), ...);
20+
os << ']';
21+
}, theTuple
22+
);
23+
return os;
24+
}
25+
26+
int test_apply()
27+
{
28+
// OK
29+
cout << std::apply(add, std::pair(1, 2)) << '\n';
30+
31+
// Error: can't deduce the function type
32+
// cout << std::apply(add_generic, std::make_pair(2.0f, 3.0f)) << '\n';
33+
34+
// OK
35+
cout << std::apply(add_lambda, std::pair(2.0f, 3.0f)) << '\n';
36+
37+
// advanced example
38+
std::tuple myTuple{25, "Hello", 9.31f, 'c'};
39+
cout << myTuple << '\n';
40+
return 0;
41+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <vector>
2+
#include <algorithm>
3+
#include <numeric>
4+
5+
#include "common.h"
6+
7+
int test_basic() {
8+
std::vector<int> x = {8, 4, 5, 9};
9+
x.push_back(std::move(6));
10+
x.push_back(9);
11+
x[2] = -1;
12+
std::sort(x.begin(), x.end());
13+
std::vector<int> y = {7, 3};
14+
std::swap(x, y);
15+
std::swap(y.front(), y.back());
16+
x.insert(x.end(), y.rbegin(), y.rend());
17+
18+
const std::vector<int> x_truth = {7, 3, -1, 9, 8, 6, 4, 9};
19+
constexpr int x_sum = 45;
20+
21+
C((x.size() == x_truth.size()));
22+
C((y.size() == x_truth.size() - 2));
23+
C((std::accumulate(x.begin(), x.end(), decltype(x)::value_type(0)) == x_sum));
24+
C((std::equal(x.begin(), x.end(), x_truth.begin())));
25+
C((std::equal(y.crbegin(), y.crend(), x_truth.cbegin() + 2)));
26+
27+
return 0;
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <charconv>
2+
#include <string_view>
3+
#include <system_error>
4+
5+
#include "common.h"
6+
7+
void show_to_chars(auto... format_args) {
8+
const size_t buf_size = 10;
9+
char buf[buf_size]{};
10+
std::to_chars_result result = std::to_chars(buf, buf + buf_size, format_args...);
11+
12+
if (result.ec != std::errc()) {
13+
cout << std::make_error_code(result.ec).message() << '\n';
14+
} else {
15+
std::string_view str(buf, result.ptr - buf);
16+
cout << str << '\n';
17+
}
18+
}
19+
20+
int test_charconv() {
21+
show_to_chars(42);
22+
// These will blow up the binary size
23+
#if 0
24+
show_to_chars(+3.14159F);
25+
show_to_chars(-3.14159, std::chars_format::fixed);
26+
show_to_chars(-3.14159, std::chars_format::scientific, 3);
27+
show_to_chars(3.1415926535, std::chars_format::fixed, 10);
28+
#endif
29+
return 0;
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <map>
2+
#include <random>
3+
#include <string>
4+
#include <chrono>
5+
6+
#include "common.h"
7+
8+
int test_random()
9+
{
10+
uint64_t seed = static_cast<uint64_t>(
11+
std::chrono::high_resolution_clock::now().time_since_epoch().count()
12+
);
13+
std::mt19937 gen(seed);
14+
15+
// If an event occurs 3 times a minute on average, how
16+
// often is it that it occurs n times in one minute?
17+
std::poisson_distribution<> d(3);
18+
19+
std::map<int, int> hist;
20+
for (int n = 0; n != 64; ++n) {
21+
++hist[d(gen)];
22+
}
23+
24+
for (auto [x, y] : hist) {
25+
printf("%2d %s\n", x, std::string(y, '*').c_str());
26+
}
27+
return 0;
28+
}

0 commit comments

Comments
 (0)