Skip to content

Commit c808f50

Browse files
authored
Merge pull request #14 from Erlkoenig90/unittest
Added simple unit test via GitHub Actions for Windows, OS X & Linux
2 parents 241e98d + c8c4589 commit c808f50

File tree

4 files changed

+259
-0
lines changed

4 files changed

+259
-0
lines changed

.github/workflows/cmake_build.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) 2019-2020-2021 Luca Cappa
2+
# Released under the term specified in file LICENSE.txt
3+
# SPDX short identifier: MIT
4+
name: mapmacro-test
5+
on: [push, pull_request]
6+
7+
jobs:
8+
buildtest:
9+
name: ${{ matrix.os }}-hosted-basic
10+
runs-on: ${{ matrix.os }}
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
os: [windows-latest, ubuntu-latest, macos-latest]
15+
include:
16+
- vcpkgCommitId: 'a9eee3b18df395dbb8be71a31bd78ea441056e42'
17+
steps:
18+
- uses: actions/checkout@v1
19+
with:
20+
submodules: true
21+
22+
- uses: lukka/get-cmake@v4.0.2
23+
24+
- name: Setup vcpkg
25+
uses: lukka/run-vcpkg@v11
26+
id: runvcpkg
27+
with:
28+
# This specifies the location of vcpkg, where it is going to be restored from cache, or create from scratch.
29+
vcpkgDirectory: '${{ github.workspace }}/vcpkg'
30+
# The Git commit id of vcpkg to be checked out. This is only needed because we are not using a submodule.
31+
vcpkgGitCommitId: '${{ matrix.vcpkgCommitId}}'
32+
# The vcpkg.json file, which will be part of cache key computation.
33+
vcpkgJsonGlob: '**/cmakepresets/vcpkg.json'
34+
- name: Run CMake consuming CMakePreset.json and run vcpkg to build packages
35+
uses: lukka/run-cmake@v10.8
36+
with:
37+
# This is the name of the CMakePresets.json's configuration to use to generate
38+
# the project files. This configuration leverages the vcpkg.cmake toolchain file to
39+
# run vcpkg and install all dependencies specified in vcpkg.json.
40+
configurePreset: 'ninja-multi-vcpkg'
41+
42+
# This is the name of the CMakePresets.json's configuration to build the project.
43+
buildPreset: 'ninja-multi-vcpkg'
44+
45+
# This is the name of the CMakePresets.json's configuration to test the project with.
46+
testPreset: 'ninja-multi-vcpkg'
47+
testPresetAdditionalArgs: "['-C', 'Debug']"

CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(MapMacroTestsuite)
4+
enable_testing()
5+
6+
add_custom_command(
7+
OUTPUT autotest.c
8+
COMMAND python ${CMAKE_SOURCE_DIR}/gentest.py ${CMAKE_CURRENT_BINARY_DIR}/autotest.c
9+
DEPENDS gentest.py
10+
)
11+
12+
set(CMAKE_C_STANDARD 99)
13+
set(CMAKE_C_STANDARD_REQUIRED True)
14+
15+
add_executable(MapMacroTestsuite ${CMAKE_CURRENT_BINARY_DIR}/autotest.c ${CMAKE_SOURCE_DIR}/map.h)
16+
17+
target_include_directories(MapMacroTestsuite PRIVATE ${CMAKE_SOURCE_DIR})
18+
19+
20+
add_test(NAME MapMacroTestsuite COMMAND MapMacroTestsuite)
21+

CMakePresets.json

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
{
2+
"version": 6,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 21,
6+
"patch": 0
7+
},
8+
"configurePresets": [
9+
{
10+
"name": "ninja",
11+
"displayName": "Ninja Configure Settings",
12+
"description": "Sets build and install directories",
13+
"binaryDir": "${sourceDir}/builds/${presetName}",
14+
"generator": "Ninja"
15+
},
16+
{
17+
"name": "ninja-toolchain",
18+
"displayName": "Ninja Configure Settings with toolchain",
19+
"description": "Sets build and install directories",
20+
"binaryDir": "${sourceDir}/builds/${presetName}-toolchain",
21+
"generator": "Ninja",
22+
"toolchainFile": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake"
23+
},
24+
{
25+
"name": "ninja-multi-vcpkg",
26+
"displayName": "Ninja Multi-Config Configure Settings",
27+
"description": "Configure with vcpkg toolchain",
28+
"binaryDir": "${sourceDir}/builds/${presetName}",
29+
"generator": "Ninja Multi-Config",
30+
"cacheVariables": {
31+
"CMAKE_TOOLCHAIN_FILE": {
32+
"type": "FILEPATH",
33+
"value": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake"
34+
}
35+
}
36+
},
37+
{
38+
"name": "msbuild-vcpkg",
39+
"displayName": "MSBuild (vcpkg toolchain) Configure Settings",
40+
"description": "Configure with VS generators and with vcpkg toolchain",
41+
"binaryDir": "${sourceDir}/builds/${presetName}",
42+
"generator": "Visual Studio 17 2022",
43+
"architecture": {
44+
"strategy": "set",
45+
"value": "x64"
46+
},
47+
"cacheVariables": {
48+
"CMAKE_TOOLCHAIN_FILE": {
49+
"type": "FILEPATH",
50+
"value": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake"
51+
}
52+
}
53+
}
54+
],
55+
"buildPresets": [
56+
{
57+
"name": "ninja",
58+
"configurePreset": "ninja",
59+
"displayName": "Build with Ninja",
60+
"description": "Build with Ninja"
61+
},
62+
{
63+
"name": "ninja-multi-vcpkg",
64+
"configurePreset": "ninja-multi-vcpkg",
65+
"displayName": "Build ninja-multi-vcpkg",
66+
"description": "Build ninja-multi-vcpkg Configurations"
67+
},
68+
{
69+
"name": "ninja-toolchain",
70+
"configurePreset": "ninja-toolchain",
71+
"displayName": "Build ninja-toolchain",
72+
"description": "Build ninja with a toolchain"
73+
},
74+
{
75+
"name": "msbuild-vcpkg",
76+
"configurePreset": "msbuild-vcpkg",
77+
"displayName": "Build MSBuild",
78+
"description": "Build with MSBuild (VS)"
79+
}
80+
],
81+
"testPresets": [
82+
{
83+
"name": "ninja",
84+
"configurePreset": "ninja"
85+
},
86+
{
87+
"name": "ninja-multi-vcpkg",
88+
"configurePreset": "ninja-multi-vcpkg"
89+
},
90+
{
91+
"name": "default-vs",
92+
"configurePreset": "msbuild-vcpkg"
93+
}
94+
],
95+
"workflowPresets": [
96+
{
97+
"name": "ninja-workflow",
98+
"steps": [
99+
{
100+
"type": "configure",
101+
"name": "ninja-multi-vcpkg"
102+
},
103+
{
104+
"type": "build",
105+
"name": "ninja-multi-vcpkg"
106+
},
107+
{
108+
"type": "test",
109+
"name": "ninja-multi-vcpkg"
110+
}
111+
]
112+
}
113+
],
114+
"packagePresets": [
115+
{
116+
"packageName": "default-package-name",
117+
"packageVersion": "0.1",
118+
"name": "default",
119+
"configurePreset": "ninja",
120+
"generators": [
121+
"TGZ"
122+
]
123+
},
124+
{
125+
"packageName": "default-multi-package-name",
126+
"packageVersion": "0.1",
127+
"name": "ninja-multi-vcpkg",
128+
"configurePreset": "ninja-multi-vcpkg",
129+
"generators": [
130+
"TGZ"
131+
],
132+
"configurations": [
133+
"Debug"
134+
]
135+
}
136+
]
137+
}

gentest.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/python3
2+
3+
import sys
4+
5+
max = 365
6+
7+
file = open(sys.argv[1], "w")
8+
9+
file.write("""#include <stdio.h>
10+
#include <assert.h>
11+
#include <string.h>
12+
#include <stddef.h>
13+
#include "map.h"
14+
15+
#define M0(x) "[" #x "]",
16+
#define M1(x) "[" #x "]"
17+
#define M2(x) x
18+
#define M3(x,y) "[" #x "," #y "]",
19+
#define M4(x,y) "[" #x "," #y "]"
20+
#define M5(x,y,z) "[" #x "," #y "," #z "]" ,
21+
#define M6(x,y,z) "[" #x "," #y "," #z "]"
22+
23+
24+
void testArray (const char* got [], const char* expected []){
25+
for (size_t i = 0; expected [i] != NULL; ++i) {
26+
assert (strcmp (got [i], expected [i]) == 0);
27+
}
28+
}
29+
30+
int main () {
31+
""")
32+
33+
def testcase(macro,cb,append,mkelem):
34+
file.write('\t{\n\t\tconst char* got [] = { '+ macro + ' (' + cb + ',')
35+
file.write(','.join(str(x) for x in range(0,max)))
36+
file.write(') ' + append + ' };\n\t\tconst char* expected [] = {')
37+
file.write(', '.join('"' + mkelem(x) + '"' for x in range(0,max)))
38+
file.write(', NULL};\n\t\ttestArray(got,expected);\n\t}\n')
39+
40+
testcase('MAP','M0', 'NULL', lambda x : '[' + str(x) + ']')
41+
42+
testcase('MAP_LIST','M1', '', lambda x : '[' + str(x) + ']')
43+
44+
testcase('MAP_UD','M3, ud', 'NULL', lambda x : '[' + str(x) + ',ud]')
45+
46+
testcase('MAP_LIST_UD','M4, ud', '', lambda x : '[' + str(x) + ',ud]')
47+
48+
testcase('MAP_UD_I','M5, ud', 'NULL', lambda x : '[' + str(x) + ',ud,' + str(x) + ']')
49+
50+
testcase('MAP_LIST_UD_I','M6, ud', '', lambda x : '[' + str(x) + ',ud,' + str(x) + ']')
51+
52+
53+
file.write('\tputs("All tests succeeded.");\n\treturn 0;\n}\n')
54+

0 commit comments

Comments
 (0)