Skip to content

Commit

Permalink
Test: Add upstream tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JGRennison committed Dec 13, 2023
1 parent c49c373 commit fceeaa4
Show file tree
Hide file tree
Showing 25 changed files with 1,215 additions and 141 deletions.
1 change: 1 addition & 0 deletions src/fontcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void UpdateFontHeightCache();

/** Font cache for basic fonts. */
class FontCache {
friend class MockFontCache;
private:
static FontCache *caches[FS_END]; ///< All the font caches.
protected:
Expand Down
4 changes: 4 additions & 0 deletions src/os/macosx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ add_files(
survey_osx.cpp
CONDITION APPLE
)

if(APPLE)
target_sources(openttd PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/osx_main.cpp)
endif()
51 changes: 51 additions & 0 deletions src/os/macosx/osx_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file unix_main.cpp Main entry for Mac OSX. */

#include "../../stdafx.h"
#include "../../openttd.h"
#include "../../crashlog.h"
#include "../../core/random_func.hpp"
#include "../../string_func.h"
#include "../../thread.h"

#include <time.h>
#include <signal.h>

#include "macos.h"

#include "../../safeguards.h"

void CocoaSetupAutoreleasePool();
void CocoaReleaseAutoreleasePool();

int CDECL main(int argc, char *argv[])
{
/* Make sure our arguments contain only valid UTF-8 characters. */
for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]);

CocoaSetupAutoreleasePool();
/* This is passed if we are launched by double-clicking */
if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
argv[1] = nullptr;
argc = 1;
}

PerThreadSetupInit();
CrashLog::InitialiseCrashLog();

SetRandomSeed(time(nullptr));

signal(SIGPIPE, SIG_IGN);

int ret = openttd_main(argc, argv);

CocoaReleaseAutoreleasePool();

return ret;
}
4 changes: 4 additions & 0 deletions src/os/unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ add_files(
font_unix.cpp
CONDITION Fontconfig_FOUND
)

if(UNIX AND NOT APPLE)
target_sources(openttd PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/unix_main.cpp)
endif()
39 changes: 0 additions & 39 deletions src/os/unix/unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@
#endif

#if defined(__APPLE__)
# if defined(WITH_SDL)
/* the mac implementation needs this file included in the same file as main() */
# include <SDL.h>
# endif

# include "../macosx/macos.h"
#endif

Expand Down Expand Up @@ -229,40 +224,6 @@ void NORETURN DoOSAbort()
}
#endif

#ifdef WITH_COCOA
void CocoaSetupAutoreleasePool();
void CocoaReleaseAutoreleasePool();
#endif

int CDECL main(int argc, char *argv[])
{
/* Make sure our arguments contain only valid UTF-8 characters. */
for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]);

#ifdef WITH_COCOA
CocoaSetupAutoreleasePool();
/* This is passed if we are launched by double-clicking */
if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
argv[1] = nullptr;
argc = 1;
}
#endif
PerThreadSetupInit();
CrashLog::InitialiseCrashLog();

SetRandomSeed(time(nullptr));

signal(SIGPIPE, SIG_IGN);

int ret = openttd_main(argc, argv);

#ifdef WITH_COCOA
CocoaReleaseAutoreleasePool();
#endif

return ret;
}

#ifndef WITH_COCOA
std::optional<std::string> GetClipboardContents()
{
Expand Down
35 changes: 35 additions & 0 deletions src/os/unix/unix_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file unix_main.cpp Main entry for Unix. */

#include "../../stdafx.h"
#include "../../openttd.h"
#include "../../crashlog.h"
#include "../../core/random_func.hpp"
#include "../../string_func.h"
#include "../../thread.h"

#include <time.h>
#include <signal.h>

#include "../../safeguards.h"

int CDECL main(int argc, char *argv[])
{
/* Make sure our arguments contain only valid UTF-8 characters. */
for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]);

PerThreadSetupInit();
CrashLog::InitialiseCrashLog();

SetRandomSeed(time(nullptr));

signal(SIGPIPE, SIG_IGN);

return openttd_main(argc, argv);
}
4 changes: 4 additions & 0 deletions src/os/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ add_files(
win32.h
CONDITION WIN32
)

if(WIN32)
target_sources(openttd PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/win32_main.cpp)
endif()
70 changes: 0 additions & 70 deletions src/os/windows/win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,37 +270,6 @@ std::optional<uint64_t> FiosGetDiskFreeSpace(const std::string &path)
return std::nullopt;
}

static int ParseCommandLine(char *line, char **argv, int max_argc)
{
int n = 0;

do {
/* skip whitespace */
while (*line == ' ' || *line == '\t') line++;

/* end? */
if (*line == '\0') break;

/* special handling when quoted */
if (*line == '"') {
argv[n++] = ++line;
while (*line != '"') {
if (*line == '\0') return n;
line++;
}
} else {
argv[n++] = line;
while (*line != ' ' && *line != '\t') {
if (*line == '\0') return n;
line++;
}
}
*line++ = '\0';
} while (n != max_argc);

return n;
}

void CreateConsole()
{
HANDLE hand;
Expand Down Expand Up @@ -419,45 +388,6 @@ void ShowInfo(const char *str)
}
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int argc;
char *argv[64]; // max 64 command line arguments

/* Set system timer resolution to 1ms. */
timeBeginPeriod(1);

PerThreadSetupInit();
CrashLog::InitialiseCrashLog();

/* Convert the command line to UTF-8. */
std::string cmdline = FS2OTTD(GetCommandLine());

/* Set the console codepage to UTF-8. */
SetConsoleOutputCP(CP_UTF8);

#if defined(_DEBUG)
CreateConsole();
#endif

_set_error_mode(_OUT_TO_MSGBOX); // force assertion output to messagebox

/* setup random seed to something quite random */
SetRandomSeed(GetTickCount());

argc = ParseCommandLine(cmdline.data(), argv, lengthof(argv));

/* Make sure our arguments contain only valid UTF-8 characters. */
for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]);

openttd_main(argc, argv);

/* Restore system timer resolution. */
timeEndPeriod(1);

return 0;
}

char *getcwd(char *buf, size_t size)
{
wchar_t path[MAX_PATH];
Expand Down
92 changes: 92 additions & 0 deletions src/os/windows/win32_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file win32_main.cpp Implementation main for Windows. */

#include "../../stdafx.h"
#include <windows.h>
#include <mmsystem.h>
#include "../../openttd.h"
#include "../../core/random_func.hpp"
#include "../../string_func.h"
#include "../../crashlog.h"
#include "../../debug.h"
#include "../../thread.h"

#include "../../safeguards.h"

static int ParseCommandLine(char *line, char **argv, int max_argc)
{
int n = 0;

do {
/* skip whitespace */
while (*line == ' ' || *line == '\t') line++;

/* end? */
if (*line == '\0') break;

/* special handling when quoted */
if (*line == '"') {
argv[n++] = ++line;
while (*line != '"') {
if (*line == '\0') return n;
line++;
}
} else {
argv[n++] = line;
while (*line != ' ' && *line != '\t') {
if (*line == '\0') return n;
line++;
}
}
*line++ = '\0';
} while (n != max_argc);

return n;
}

void CreateConsole();

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int argc;
char *argv[64]; // max 64 command line arguments

/* Set system timer resolution to 1ms. */
timeBeginPeriod(1);

PerThreadSetupInit();
CrashLog::InitialiseCrashLog();

/* Convert the command line to UTF-8. */
std::string cmdline = FS2OTTD(GetCommandLine());

/* Set the console codepage to UTF-8. */
SetConsoleOutputCP(CP_UTF8);

#if defined(_DEBUG)
CreateConsole();
#endif

_set_error_mode(_OUT_TO_MSGBOX); // force assertion output to messagebox

/* setup random seed to something quite random */
SetRandomSeed(GetTickCount());

argc = ParseCommandLine(cmdline.data(), argv, lengthof(argv));

/* Make sure our arguments contain only valid UTF-8 characters. */
for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]);

openttd_main(argc, argv);

/* Restore system timer resolution. */
timeEndPeriod(1);

return 0;
}
4 changes: 2 additions & 2 deletions src/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@

#endif /* defined(_MSC_VER) */

#if !defined(STRGEN) && !defined(SETTINGSGEN) && !defined(OPENTTD_TEST)
#if !defined(STRGEN) && !defined(SETTINGSGEN)
# if defined(_WIN32)
char *getcwd(char *buf, size_t size);
# include <io.h>
Expand All @@ -220,7 +220,7 @@
template <typename T> std::string FS2OTTD(T name) { return name; }
template <typename T> std::string OTTD2FS(T name) { return name; }
# endif /* _WIN32 or WITH_ICONV */
#endif /* STRGEN || SETTINGSGEN || OPENTTD_TEST */
#endif /* STRGEN || SETTINGSGEN */

#if defined(_WIN32)
# define PATHSEP "\\"
Expand Down
12 changes: 8 additions & 4 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ add_test_files(
bitmath_func.cpp
landscape_partial_pixel_z.cpp
math_func.cpp
mock_environment.h
mock_fontcache.h
mock_spritecache.cpp
mock_spritecache.h
ring_buffer.cpp
string_func.cpp
strings_func.cpp
test_main.cpp
../landscape_ppz.cpp
../core/alloc_func.cpp
../core/bitmath_func.cpp
../core/math_func.cpp
test_script_admin.cpp
test_window_desc.cpp
)
1 change: 0 additions & 1 deletion src/tests/bitmath_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

/** @file bitmath_func_test.cpp Test functionality from core/bitmath_func. */

#define OPENTTD_TEST
#include "../stdafx.h"

#include "../3rdparty/catch2/catch.hpp"
Expand Down
Loading

0 comments on commit fceeaa4

Please sign in to comment.