Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/os/test_os.h
10278 views
1
/**************************************************************************/
2
/* test_os.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/os/os.h"
34
35
#include "thirdparty/doctest/doctest.h"
36
37
namespace TestOS {
38
39
TEST_CASE("[OS] Environment variables") {
40
#ifdef WINDOWS_ENABLED
41
CHECK_MESSAGE(
42
OS::get_singleton()->has_environment("USERPROFILE"),
43
"The USERPROFILE environment variable should be present.");
44
#else
45
CHECK_MESSAGE(
46
OS::get_singleton()->has_environment("HOME"),
47
"The HOME environment variable should be present.");
48
#endif
49
}
50
51
TEST_CASE("[OS] UTF-8 environment variables") {
52
String value = String::utf8("hell\xc3\xb6"); // "hellö", UTF-8 encoded
53
54
OS::get_singleton()->set_environment("HELLO", value);
55
String val = OS::get_singleton()->get_environment("HELLO");
56
CHECK_MESSAGE(
57
val == value,
58
"The previously-set HELLO environment variable should return the expected value.");
59
CHECK_MESSAGE(
60
val.length() == 5,
61
"The previously-set HELLO environment variable was decoded as UTF-8 and should have a length of 5.");
62
OS::get_singleton()->unset_environment("HELLO");
63
}
64
65
TEST_CASE("[OS] Non-UTF-8 environment variables") {
66
String value = String("\xff t\xf6rkylempij\xe4vongahdus"); // hex FF and a Finnish pangram, latin-1
67
OS::get_singleton()->set_environment("HELLO", value);
68
String val = OS::get_singleton()->get_environment("HELLO");
69
CHECK_MESSAGE(
70
val == value,
71
"The previously-set HELLO environment variable should return the expected value.");
72
CHECK_MESSAGE(
73
val.length() == 23,
74
"The previously-set HELLO environment variable was not decoded from Latin-1.");
75
OS::get_singleton()->unset_environment("HELLO");
76
}
77
78
TEST_CASE("[OS] Command line arguments") {
79
List<String> arguments = OS::get_singleton()->get_cmdline_args();
80
bool found = false;
81
for (const String &arg : arguments) {
82
if (arg == "--test") {
83
found = true;
84
break;
85
}
86
}
87
CHECK_MESSAGE(
88
found,
89
"The `--test` option must be present in the list of command line arguments.");
90
}
91
92
TEST_CASE("[OS] Executable and data paths") {
93
CHECK_MESSAGE(
94
OS::get_singleton()->get_executable_path().is_absolute_path(),
95
"The executable path returned should be an absolute path.");
96
CHECK_MESSAGE(
97
OS::get_singleton()->get_data_path().is_absolute_path(),
98
"The user data path returned should be an absolute path.");
99
CHECK_MESSAGE(
100
OS::get_singleton()->get_config_path().is_absolute_path(),
101
"The user configuration path returned should be an absolute path.");
102
CHECK_MESSAGE(
103
OS::get_singleton()->get_cache_path().is_absolute_path(),
104
"The cache path returned should be an absolute path.");
105
}
106
107
TEST_CASE("[OS] Ticks") {
108
CHECK_MESSAGE(
109
OS::get_singleton()->get_ticks_usec() > 1000,
110
"The returned ticks (in microseconds) must be greater than 1,000.");
111
CHECK_MESSAGE(
112
OS::get_singleton()->get_ticks_msec() > 1,
113
"The returned ticks (in milliseconds) must be greater than 1.");
114
}
115
116
TEST_CASE("[OS] Feature tags") {
117
#ifdef TOOLS_ENABLED
118
CHECK_MESSAGE(
119
OS::get_singleton()->has_feature("editor"),
120
"The binary has the \"editor\" feature tag.");
121
CHECK_MESSAGE(
122
!OS::get_singleton()->has_feature("template"),
123
"The binary does not have the \"template\" feature tag.");
124
CHECK_MESSAGE(
125
!OS::get_singleton()->has_feature("template_debug"),
126
"The binary does not have the \"template_debug\" feature tag.");
127
CHECK_MESSAGE(
128
!OS::get_singleton()->has_feature("template_release"),
129
"The binary does not have the \"template_release\" feature tag.");
130
#else
131
CHECK_MESSAGE(
132
!OS::get_singleton()->has_feature("editor"),
133
"The binary does not have the \"editor\" feature tag.");
134
CHECK_MESSAGE(
135
OS::get_singleton()->has_feature("template"),
136
"The binary has the \"template\" feature tag.");
137
#ifdef DEBUG_ENABLED
138
CHECK_MESSAGE(
139
OS::get_singleton()->has_feature("template_debug"),
140
"The binary has the \"template_debug\" feature tag.");
141
CHECK_MESSAGE(
142
!OS::get_singleton()->has_feature("template_release"),
143
"The binary does not have the \"template_release\" feature tag.");
144
#else
145
CHECK_MESSAGE(
146
!OS::get_singleton()->has_feature("template_debug"),
147
"The binary does not have the \"template_debug\" feature tag.");
148
CHECK_MESSAGE(
149
OS::get_singleton()->has_feature("template_release"),
150
"The binary has the \"template_release\" feature tag.");
151
#endif // DEBUG_ENABLED
152
#endif // TOOLS_ENABLED
153
}
154
155
TEST_CASE("[OS] Process ID") {
156
CHECK_MESSAGE(
157
OS::get_singleton()->get_process_id() >= 1,
158
"The returned process ID should be greater than zero.");
159
}
160
161
TEST_CASE("[OS] Processor count and memory information") {
162
CHECK_MESSAGE(
163
OS::get_singleton()->get_processor_count() >= 1,
164
"The returned processor count should be greater than zero.");
165
#ifdef DEBUG_ENABLED
166
CHECK_MESSAGE(
167
OS::get_singleton()->get_static_memory_usage() >= 1,
168
"The returned static memory usage should be greater than zero.");
169
CHECK_MESSAGE(
170
OS::get_singleton()->get_static_memory_peak_usage() >= 1,
171
"The returned static memory peak usage should be greater than zero.");
172
#endif // DEBUG_ENABLED
173
}
174
175
TEST_CASE("[OS] Execute") {
176
#ifdef WINDOWS_ENABLED
177
List<String> arguments;
178
arguments.push_back("/C");
179
arguments.push_back("dir > NUL");
180
int exit_code;
181
const Error err = OS::get_singleton()->execute("cmd", arguments, nullptr, &exit_code);
182
CHECK_MESSAGE(
183
err == OK,
184
"(Running the command `cmd /C \"dir > NUL\"` returns the expected Godot error code (OK).");
185
CHECK_MESSAGE(
186
exit_code == 0,
187
"Running the command `cmd /C \"dir > NUL\"` returns a zero (successful) exit code.");
188
#else
189
List<String> arguments;
190
arguments.push_back("-c");
191
arguments.push_back("ls > /dev/null");
192
int exit_code;
193
const Error err = OS::get_singleton()->execute("sh", arguments, nullptr, &exit_code);
194
CHECK_MESSAGE(
195
err == OK,
196
"(Running the command `sh -c \"ls > /dev/null\"` returns the expected Godot error code (OK).");
197
CHECK_MESSAGE(
198
exit_code == 0,
199
"Running the command `sh -c \"ls > /dev/null\"` returns a zero (successful) exit code.");
200
#endif
201
}
202
203
} // namespace TestOS
204
205