/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2023 Cppcheck team.
*
* This program 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, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see .
*/
#include "platform.h"
#include "fixture.h"
#include
class TestPlatform : public TestFixture {
public:
TestPlatform() : TestFixture("TestPlatform") {}
private:
void run() override {
TEST_CASE(empty);
TEST_CASE(valid_config_win32a);
TEST_CASE(valid_config_unix64);
TEST_CASE(valid_config_win32w);
TEST_CASE(valid_config_unix32);
TEST_CASE(valid_config_win64);
TEST_CASE(valid_config_file_1);
TEST_CASE(valid_config_file_2);
TEST_CASE(valid_config_file_3);
TEST_CASE(valid_config_file_4);
TEST_CASE(invalid_config_file_1);
TEST_CASE(empty_elements);
TEST_CASE(default_platform);
}
static bool readPlatform(cppcheck::Platform& platform, const char* xmldata) {
tinyxml2::XMLDocument doc;
doc.Parse(xmldata);
return platform.loadFromXmlDocument(&doc);
}
void empty() const {
// An empty platform file does not change values, only the type.
const char xmldata[] = "\n";
cppcheck::Platform platform;
// TODO: this should fail - platform files need to be complete
TODO_ASSERT(!readPlatform(platform, xmldata));
}
void valid_config_win32a() const {
// Verify if native Win32A platform is loaded correctly
cppcheck::Platform platform;
PLATFORM(platform, cppcheck::Platform::Win32A);
ASSERT_EQUALS(cppcheck::Platform::Win32A, platform.platformType);
ASSERT(platform.isWindowsPlatform());
ASSERT_EQUALS(1, platform.sizeof_bool);
ASSERT_EQUALS(2, platform.sizeof_short);
ASSERT_EQUALS(4, platform.sizeof_int);
ASSERT_EQUALS(4, platform.sizeof_long);
ASSERT_EQUALS(8, platform.sizeof_long_long);
ASSERT_EQUALS(4, platform.sizeof_float);
ASSERT_EQUALS(8, platform.sizeof_double);
ASSERT_EQUALS(8, platform.sizeof_long_double);
ASSERT_EQUALS(2, platform.sizeof_wchar_t);
ASSERT_EQUALS(4, platform.sizeof_size_t);
ASSERT_EQUALS(4, platform.sizeof_pointer);
ASSERT_EQUALS('\0', platform.defaultSign);
ASSERT_EQUALS(8, platform.char_bit);
ASSERT_EQUALS(16, platform.short_bit);
ASSERT_EQUALS(32, platform.int_bit);
ASSERT_EQUALS(32, platform.long_bit);
ASSERT_EQUALS(64, platform.long_long_bit);
}
void valid_config_unix64() const {
// Verify if native Unix64 platform is loaded correctly
cppcheck::Platform platform;
PLATFORM(platform, cppcheck::Platform::Unix64);
ASSERT_EQUALS(cppcheck::Platform::Unix64, platform.platformType);
ASSERT(!platform.isWindowsPlatform());
ASSERT_EQUALS(1, platform.sizeof_bool);
ASSERT_EQUALS(2, platform.sizeof_short);
ASSERT_EQUALS(4, platform.sizeof_int);
ASSERT_EQUALS(8, platform.sizeof_long);
ASSERT_EQUALS(8, platform.sizeof_long_long);
ASSERT_EQUALS(4, platform.sizeof_float);
ASSERT_EQUALS(8, platform.sizeof_double);
ASSERT_EQUALS(16, platform.sizeof_long_double);
ASSERT_EQUALS(4, platform.sizeof_wchar_t);
ASSERT_EQUALS(8, platform.sizeof_size_t);
ASSERT_EQUALS(8, platform.sizeof_pointer);
ASSERT_EQUALS('\0', platform.defaultSign);
ASSERT_EQUALS(8, platform.char_bit);
ASSERT_EQUALS(16, platform.short_bit);
ASSERT_EQUALS(32, platform.int_bit);
ASSERT_EQUALS(64, platform.long_bit);
ASSERT_EQUALS(64, platform.long_long_bit);
}
void valid_config_win32w() const {
// Verify if native Win32W platform is loaded correctly
cppcheck::Platform platform;
PLATFORM(platform, cppcheck::Platform::Win32W);
ASSERT_EQUALS(cppcheck::Platform::Win32W, platform.platformType);
ASSERT(platform.isWindowsPlatform());
ASSERT_EQUALS(1, platform.sizeof_bool);
ASSERT_EQUALS(2, platform.sizeof_short);
ASSERT_EQUALS(4, platform.sizeof_int);
ASSERT_EQUALS(4, platform.sizeof_long);
ASSERT_EQUALS(8, platform.sizeof_long_long);
ASSERT_EQUALS(4, platform.sizeof_float);
ASSERT_EQUALS(8, platform.sizeof_double);
ASSERT_EQUALS(8, platform.sizeof_long_double);
ASSERT_EQUALS(2, platform.sizeof_wchar_t);
ASSERT_EQUALS(4, platform.sizeof_size_t);
ASSERT_EQUALS(4, platform.sizeof_pointer);
ASSERT_EQUALS('\0', platform.defaultSign);
ASSERT_EQUALS(8, platform.char_bit);
ASSERT_EQUALS(16, platform.short_bit);
ASSERT_EQUALS(32, platform.int_bit);
ASSERT_EQUALS(32, platform.long_bit);
ASSERT_EQUALS(64, platform.long_long_bit);
}
void valid_config_unix32() const {
// Verify if native Unix32 platform is loaded correctly
cppcheck::Platform platform;
PLATFORM(platform, cppcheck::Platform::Unix32);
ASSERT_EQUALS(cppcheck::Platform::Unix32, platform.platformType);
ASSERT(!platform.isWindowsPlatform());
ASSERT_EQUALS(1, platform.sizeof_bool);
ASSERT_EQUALS(2, platform.sizeof_short);
ASSERT_EQUALS(4, platform.sizeof_int);
ASSERT_EQUALS(4, platform.sizeof_long);
ASSERT_EQUALS(8, platform.sizeof_long_long);
ASSERT_EQUALS(4, platform.sizeof_float);
ASSERT_EQUALS(8, platform.sizeof_double);
ASSERT_EQUALS(12, platform.sizeof_long_double);
ASSERT_EQUALS(4, platform.sizeof_wchar_t);
ASSERT_EQUALS(4, platform.sizeof_size_t);
ASSERT_EQUALS(4, platform.sizeof_pointer);
ASSERT_EQUALS('\0', platform.defaultSign);
ASSERT_EQUALS(8, platform.char_bit);
ASSERT_EQUALS(16, platform.short_bit);
ASSERT_EQUALS(32, platform.int_bit);
ASSERT_EQUALS(32, platform.long_bit);
ASSERT_EQUALS(64, platform.long_long_bit);
}
void valid_config_win64() const {
// Verify if native Win64 platform is loaded correctly
cppcheck::Platform platform;
PLATFORM(platform, cppcheck::Platform::Win64);
ASSERT_EQUALS(cppcheck::Platform::Win64, platform.platformType);
ASSERT(platform.isWindowsPlatform());
ASSERT_EQUALS(1, platform.sizeof_bool);
ASSERT_EQUALS(2, platform.sizeof_short);
ASSERT_EQUALS(4, platform.sizeof_int);
ASSERT_EQUALS(4, platform.sizeof_long);
ASSERT_EQUALS(8, platform.sizeof_long_long);
ASSERT_EQUALS(4, platform.sizeof_float);
ASSERT_EQUALS(8, platform.sizeof_double);
ASSERT_EQUALS(8, platform.sizeof_long_double);
ASSERT_EQUALS(2, platform.sizeof_wchar_t);
ASSERT_EQUALS(8, platform.sizeof_size_t);
ASSERT_EQUALS(8, platform.sizeof_pointer);
ASSERT_EQUALS('\0', platform.defaultSign);
ASSERT_EQUALS(8, platform.char_bit);
ASSERT_EQUALS(16, platform.short_bit);
ASSERT_EQUALS(32, platform.int_bit);
ASSERT_EQUALS(32, platform.long_bit);
ASSERT_EQUALS(64, platform.long_long_bit);
}
void valid_config_file_1() {
// Valid platform configuration with all possible values specified.
// Similar to the avr8 platform file.
const char xmldata[] = "\n"
"\n"
" 8\n"
" unsigned\n"
" \n"
" 1\n"
" 2\n"
" 2\n"
" 4\n"
" 8\n"
" 4\n"
" 4\n"
" 4\n"
" 2\n"
" 2\n"
" 2\n"
" \n"
" ";
cppcheck::Platform platform;
ASSERT(readPlatform(platform, xmldata));
ASSERT_EQUALS(platform.PlatformFile, platform.platformType);
ASSERT(!platform.isWindowsPlatform());
ASSERT_EQUALS(8, platform.char_bit);
ASSERT_EQUALS('u', platform.defaultSign);
ASSERT_EQUALS(1, platform.sizeof_bool);
ASSERT_EQUALS(2, platform.sizeof_short);
ASSERT_EQUALS(2, platform.sizeof_int);
ASSERT_EQUALS(4, platform.sizeof_long);
ASSERT_EQUALS(8, platform.sizeof_long_long);
ASSERT_EQUALS(4, platform.sizeof_float);
ASSERT_EQUALS(4, platform.sizeof_double);
ASSERT_EQUALS(4, platform.sizeof_long_double);
ASSERT_EQUALS(2, platform.sizeof_pointer);
ASSERT_EQUALS(2, platform.sizeof_size_t);
ASSERT_EQUALS(2, platform.sizeof_wchar_t);
ASSERT_EQUALS(16, platform.short_bit);
ASSERT_EQUALS(16, platform.int_bit);
ASSERT_EQUALS(32, platform.long_bit);
ASSERT_EQUALS(64, platform.long_long_bit);
}
void valid_config_file_2() {
// Valid platform configuration with all possible values specified and
// char_bit > 8.
const char xmldata[] = "\n"
"\n"
" 20\n"
" signed\n"
" \n"
" 1\n"
" 2\n"
" 3\n"
" 4\n"
" 5\n"
" 6\n"
" 7\n"
" 8\n"
" 9\n"
" 10\n"
" 11\n"
" \n"
" ";
cppcheck::Platform platform;
ASSERT(readPlatform(platform, xmldata));
ASSERT_EQUALS(platform.PlatformFile, platform.platformType);
ASSERT(!platform.isWindowsPlatform());
ASSERT_EQUALS(20, platform.char_bit);
ASSERT_EQUALS('s', platform.defaultSign);
ASSERT_EQUALS(1, platform.sizeof_bool);
ASSERT_EQUALS(2, platform.sizeof_short);
ASSERT_EQUALS(3, platform.sizeof_int);
ASSERT_EQUALS(4, platform.sizeof_long);
ASSERT_EQUALS(5, platform.sizeof_long_long);
ASSERT_EQUALS(6, platform.sizeof_float);
ASSERT_EQUALS(7, platform.sizeof_double);
ASSERT_EQUALS(8, platform.sizeof_long_double);
ASSERT_EQUALS(9, platform.sizeof_pointer);
ASSERT_EQUALS(10, platform.sizeof_size_t);
ASSERT_EQUALS(11, platform.sizeof_wchar_t);
ASSERT_EQUALS(40, platform.short_bit);
ASSERT_EQUALS(60, platform.int_bit);
ASSERT_EQUALS(80, platform.long_bit);
ASSERT_EQUALS(100, platform.long_long_bit);
}
void valid_config_file_3() const {
// Valid platform configuration without any usable information.
// Similar like an empty file.
const char xmldata[] = "\n"
"\n"
" 8\n"
" unsigned\n"
" \n"
" 1\n"
" 2\n"
" 3\n"
" 4\n"
" 5\n"
" 6\n"
" 7\n"
" 8\n"
" 9\n"
" 10\n"
" 11\n"
" \n"
" ";
cppcheck::Platform platform;
// TODO: needs to fail - files need to be complete
TODO_ASSERT(!readPlatform(platform, xmldata));
}
void valid_config_file_4() {
// Valid platform configuration with all possible values specified and
// set to 0.
const char xmldata[] = "\n"
"\n"
" 0\n"
" z\n"
" \n"
" 0\n"
" 0\n"
" 0\n"
" 0\n"
" 0\n"
" 0\n"
" 0\n"
" 0\n"
" 0\n"
" 0\n"
" 0\n"
" \n"
" ";
cppcheck::Platform platform;
ASSERT(readPlatform(platform, xmldata));
ASSERT_EQUALS(platform.PlatformFile, platform.platformType);
ASSERT(!platform.isWindowsPlatform());
ASSERT_EQUALS(0, platform.char_bit);
ASSERT_EQUALS('z', platform.defaultSign);
ASSERT_EQUALS(0, platform.sizeof_bool);
ASSERT_EQUALS(0, platform.sizeof_short);
ASSERT_EQUALS(0, platform.sizeof_int);
ASSERT_EQUALS(0, platform.sizeof_long);
ASSERT_EQUALS(0, platform.sizeof_long_long);
ASSERT_EQUALS(0, platform.sizeof_float);
ASSERT_EQUALS(0, platform.sizeof_double);
ASSERT_EQUALS(0, platform.sizeof_long_double);
ASSERT_EQUALS(0, platform.sizeof_pointer);
ASSERT_EQUALS(0, platform.sizeof_size_t);
ASSERT_EQUALS(0, platform.sizeof_wchar_t);
ASSERT_EQUALS(0, platform.short_bit);
ASSERT_EQUALS(0, platform.int_bit);
ASSERT_EQUALS(0, platform.long_bit);
ASSERT_EQUALS(0, platform.long_long_bit);
}
void invalid_config_file_1() const {
// Invalid XML file: mismatching elements "boolt" vs "bool".
const char xmldata[] = "\n"
"\n"
" 8\n"
" unsigned\n"
" \n"
" 1\n"
" 2\n"
" 2\n"
" 4\n"
" 8\n"
" 4\n"
" 4\n"
" 4\n"
" 2\n"
" 2\n"
" 2\n"
" \n"
" ";
cppcheck::Platform platform;
ASSERT(!readPlatform(platform, xmldata));
}
void empty_elements() const {
// Valid platform configuration without any usable information.
// Similar like an empty file.
const char xmldata[] = "\n"
"\n"
" \n"
" \n"
" \n"
" \n"
" \n"
" \n"
" \n"
" \n"
" \n"
" \n"
" \n"
" \n"
" \n"
" \n"
" \n"
" ";
cppcheck::Platform platform;
ASSERT(!readPlatform(platform, xmldata));
}
void default_platform() {
cppcheck::Platform platform;
#if defined(_WIN64)
ASSERT_EQUALS(cppcheck::Platform::Win64, platform.platformType);
#elif defined(_WIN32)
ASSERT_EQUALS(cppcheck::Platform::Win32A, platform.platformType);
#else
ASSERT_EQUALS(cppcheck::Platform::Native, platform.platformType);
#endif
}
};
REGISTER_TEST(TestPlatform)