Repository navigation
Expand file tree
/
Copy pathFoundationTest.cpp
More file actions
177 lines (157 loc) · 6.04 KB
/
Copy pathFoundationTest.cpp
File metadata and controls
177 lines (157 loc) · 6.04 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <cstdio>
#include <limits>
#include <mutex>
#include <string>
#include <vector>
#include "FakePlatform.h"
#include "ns/Deadline.h"
#include "ns/IsolateStateBag.h"
#include "ns/Thread.h"
#include "ns/Utf8.h"
#include "ns/V8Flags.h"
using namespace nativescript::common;
namespace {
int failures = 0;
void Check(bool ok, const char* what) {
if (!ok) {
failures++;
std::fprintf(stderr, "FAIL %s\n", what);
}
}
void TestDeadline() {
using std::chrono::milliseconds;
Check(DeadlinePassed(DeadlineAfterSeconds(0)), "zero has passed");
Check(DeadlinePassed(DeadlineAfterSeconds(-1)), "negative has passed");
Check(DeadlinePassed(
DeadlineAfterSeconds(std::numeric_limits<double>::quiet_NaN())),
"NaN has passed");
Check(!DeadlineAfterSeconds(std::numeric_limits<double>::infinity()),
"infinity is unbounded");
Check(!DeadlineAfterSeconds(1e12), "beyond the clock is unbounded");
Check(!DeadlinePassed(std::nullopt), "unbounded never passes");
const Deadline soon = DeadlineAfterSeconds(10);
Check(!DeadlinePassed(soon), "10 s is ahead");
const milliseconds left = RemainingMs(soon, milliseconds(60000));
Check(left > milliseconds(9000) && left <= milliseconds(10000),
"remaining ~10 s");
Check(RemainingMs(soon, milliseconds(5)) == milliseconds(5), "capped");
Check(RemainingMs(std::nullopt, milliseconds(7)) == milliseconds(7),
"unbounded gives the cap");
Check(
RemainingMs(DeadlineAfterSeconds(0), milliseconds(7)) == milliseconds(0),
"passed gives zero");
const Deadline micro =
std::chrono::steady_clock::now() + std::chrono::microseconds(1500 * 1000);
Check(RemainingMs(micro, milliseconds(60000)) >= milliseconds(1),
"rounds up, never down to a premature wake");
}
void TestV8Flags() {
Check(!V8FlagsDisableLazyCompile(""), "lazy by default");
Check(V8FlagsDisableLazyCompile("--no-lazy"), "--no-lazy");
Check(V8FlagsDisableLazyCompile("--nolazy"), "--nolazy");
Check(V8FlagsDisableLazyCompile("-no_lazy"), "-no_lazy");
Check(!V8FlagsDisableLazyCompile("--no-lazy --lazy"), "last wins");
Check(!V8FlagsDisableLazyCompile("--no-lazy-feedback-allocation"),
"a longer flag is not --no-lazy");
Check(!V8FlagsDisableLazyCompile("-- --no-lazy"), "nothing after --");
Check(V8FlagsStackSizeKb("") == 0, "no stack size");
Check(V8FlagsStackSizeKb("--stack-size=1500") == 1500, "= form");
Check(V8FlagsStackSizeKb("--stack_size 900") == 900, "next-arg form");
Check(V8FlagsStackSizeKb("--stack-size=1 --stack-size=2") == 2, "last wins");
Check(V8FlagsStackSizeKb("--stack-size=abc") == 0, "unparsable");
Check(V8FlagsStackSizeKb("--stack-size=-5") == 0, "negative");
Check(V8FlagsStackSizeKb("--stack-size") == 0, "missing value");
Check(BackgroundCompileStackBytes("") == 2 * 1024 * 1024, "2 MB floor");
Check(BackgroundCompileStackBytes("--stack-size=100") == 2 * 1024 * 1024,
"small stays at the floor");
Check(BackgroundCompileStackBytes("--stack-size=4096") ==
4096 * 1024 + 1024 * 1024,
"stack plus headroom");
}
std::vector<uint16_t> Units(std::u16string_view s) {
return std::vector<uint16_t>(s.begin(), s.end());
}
void TestUtf8() {
auto exact = [](std::u16string_view s) {
std::vector<uint16_t> units = Units(s);
return Utf16ToExactUtf8(units.data(), units.size());
};
Check(exact(u"abc") == std::string("abc"), "ascii");
Check(exact(u"é") == std::string("\xc3\xa9"), "two bytes");
Check(exact(u"€") == std::string("\xe2\x82\xac"), "three bytes");
Check(exact(u"\U0001F600") == std::string("\xf0\x9f\x98\x80"), "four bytes");
const std::u16string withNul(u"a\0b", 3);
Check(exact(withNul) == std::string("a\0b", 3), "U+0000 is one byte");
const uint16_t lone[] = {0xD800};
Check(!Utf16ToExactUtf8(lone, 1), "lone high surrogate");
const uint16_t trail[] = {0xDC00, 0x41};
Check(!Utf16ToExactUtf8(trail, 2), "lone low surrogate");
Check(Utf16ToExactUtf8(nullptr, 0) == std::string(), "empty");
}
int destroyed = 0;
struct StateA {
int value = 1;
~StateA() { destroyed++; }
};
struct StateB {
std::string value = "b";
~StateB() { destroyed++; }
};
void TestIsolateStateBag() {
destroyed = 0;
{
IsolateStateBag bag;
Check(bag.Existing<StateA>() == nullptr, "nothing before first use");
StateA* a = bag.Get<StateA>();
Check(a != nullptr && a->value == 1, "created default");
Check(bag.Get<StateA>() == a, "same instance");
Check(bag.Existing<StateA>() == a, "existing finds it");
Check(bag.Existing<StateB>() == nullptr, "other types stay absent");
bag.Get<StateB>()->value = "x";
IsolateStateBag other;
Check(other.Get<StateA>() != a, "bags share nothing");
bag.Close();
Check(destroyed == 1 + 1, "close destroys the bag's states");
Check(bag.IsClosed(), "closed");
Check(bag.Get<StateA>() == nullptr, "no creation after close");
Check(bag.Existing<StateB>() == nullptr, "nothing left after close");
bag.Close();
Check(destroyed == 2, "close is idempotent");
}
Check(destroyed == 3, "the other bag's state dies with it");
}
void TestStartThread() {
std::mutex mutex;
std::condition_variable cv;
bool ran = false;
const int priorityBefore = fake_platform::threadPriorityCalls.load();
const bool started = StartThread(
ThreadSpec{"ns-test", 4 * 1024 * 1024, ThreadPriority::kUtility}, [&] {
std::lock_guard<std::mutex> lock(mutex);
ran = true;
cv.notify_all();
});
Check(started, "thread started");
std::unique_lock<std::mutex> lock(mutex);
Check(cv.wait_for(lock, std::chrono::seconds(10), [&] { return ran; }),
"body ran");
Check(fake_platform::threadPriorityCalls.load() == priorityBefore + 1,
"priority applied on the new thread");
}
} // namespace
int main() {
TestDeadline();
TestV8Flags();
TestUtf8();
TestIsolateStateBag();
TestStartThread();
if (failures > 0) {
std::fprintf(stderr, "%d failure(s)\n", failures);
return 1;
}
std::printf("all foundation tests passed\n");
return 0;
}