Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

curious if it's possible to run matoya tests in CI #115

Open
wants to merge 21 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Tests
on:
pull_request:
types: [ labeled, synchronize ]
jobs:
label:
name: Find 'Test Matoya' Label
runs-on: ubuntu-latest
outputs:
label_exists: ${{contains(toJson(github.event.label.name), 'Test Matoya') || ( (github.event.action == 'synchronize') && contains(toJson(github.event.pull_request.labels.*.name), 'Test Matoya') )}}
steps:
- name: Nothing
shell: bash
run: echo ""
macos:
name: macOS
runs-on: macos-latest
needs: label
if: ${{needs.label.outputs.label_exists == 'true'}}
permissions:
contents: read
env:
TERM: xterm-256color
steps:
- name: Checkout Branch
uses: actions/checkout@v4
with:
path: libmatoya
- name: Test
run: |
cd libmatoya
make ARCH=arm64
cd test
make ARCH=arm64
linux:
name: Linux
runs-on: ubuntu-latest
needs: label
if: ${{needs.label.outputs.label_exists == 'true'}}
permissions:
contents: read
env:
TERM: xterm-256color
steps:
- name: Checkout Branch
uses: actions/checkout@v4
with:
path: libmatoya
- name: Test
run: |
cd libmatoya
make
cd test
make
windows:
name: "Windows x64"
runs-on: windows-2022
needs: label
if: ${{needs.label.outputs.label_exists == 'true'}}
permissions:
contents: read
steps:
- name: Checkout Branch
uses: actions/checkout@v4
with:
path: libmatoya
- name: Test
shell: cmd
run: |
call "C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Auxiliary/Build/vcvars64.bat"
cd libmatoya
nmake
cd test
nmake
10 changes: 10 additions & 0 deletions test/src/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// If a copy of the MIT License was not distributed with this file,
// You can obtain one at https://spdx.org/licenses/MIT.html.

#define _CRT_SECURE_NO_WARNINGS

#include "matoya.h"

#define _USE_MATH_DEFINES
Expand Down Expand Up @@ -29,6 +31,8 @@
#include "test/crypto.h"
#include "test/net.h"

uint64_t test_seed = 0xDEADBEEFCAFEBABEUL;

static void main_log(const char *msg, void *opaque)
{
printf("%s\n", msg);
Expand All @@ -40,6 +44,12 @@ int32_t main(int32_t argc, char **argv)

MTY_SetLogFunc(main_log, NULL);

test_seed ^= MTY_GetTime();
const char *seed = getenv("MATOYA_TEST_SEED");
if (seed)
sscanf(seed, "0x%" PRIx64, &test_seed);
printf("::notice file=%s,line=%d::MATOYA_TEST_SEED=0x%" PRIx64 "\n", __FILE__, __LINE__, test_seed);

if (!json_main())
return 1;

Expand Down
18 changes: 10 additions & 8 deletions test/src/test/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ static const uint8_t JSON_UTF8[] = {
0xA7, 0xE0, 0xA2, 0xA8, 0x09, 0x0A, 0xF0, 0x9F, 0x86, 0x80, 0xF0, 0x9F, 0x86,
0x81, 0xF0, 0x9F, 0x86, 0x82, 0xF0, 0x9F, 0x86, 0x83, 0xF0, 0x9F, 0x86, 0x84,
0xF0, 0x9F, 0x86, 0x85, 0xF0, 0x9F, 0x86, 0x86, 0xF0, 0x9F, 0x86, 0x87, 0xF0,
0x9F, 0x86, 0x88, 0xF0, 0x9F, 0x86, 0x89, 0x00
0x9F, 0x86, 0x88, 0xF0, 0x9F, 0x86, 0x89,
0xF4, 0x8F, 0xBF, 0xBF, 0x00 // maximum code point: 0x10FFFF
};

static const char *JSON_UTF16 = "\""
Expand All @@ -29,15 +30,16 @@ static const char *JSON_UTF16 = "\""
"\\u08a2\\u08a3\\u08a4\\u08a5\\u08a6\\u08a7\\u08a8\\u0009\\u000a\\ud83c"
"\\udd80\\ud83c\\udd81\\ud83c\\udd82\\ud83c\\udd83\\ud83c\\udd84\\ud83c"
"\\udd85\\ud83c\\udd86\\ud83c\\udd87\\ud83c\\udd88\\ud83c\\udd89"
"\\udbff\\udfff" // maximum code point: 0x10FFFF
"\"";

static char *json_random_string(void)
{
uint32_t len = MTY_GetRandomUInt(JSON_STRING_MIN, JSON_STRING_MAX);
uint32_t len = test_GetRandomUInt(JSON_STRING_MIN, JSON_STRING_MAX);
char *str = MTY_Alloc(len + 1, 1);

for (uint32_t x = 0; x < len; x++)
str[x] = (char) MTY_GetRandomUInt(1, 128);
str[x] = (char) test_GetRandomUInt(1, 128);

return str;
}
Expand All @@ -46,19 +48,19 @@ static MTY_JSON *json_random(uint32_t *n)
{
MTY_JSON *j = NULL;

uint32_t action = MTY_GetRandomUInt(0, 6);
uint32_t action = test_GetRandomUInt(0, 6);

switch (action) {
case 0: {
uint32_t size = MTY_GetRandomUInt(0, JSON_ARRAY_MAX);
uint32_t size = test_GetRandomUInt(0, JSON_ARRAY_MAX);
j = MTY_JSONArrayCreate(size);

for (uint32_t x = 0; x < size && *n < JSON_ITEM_MAX; x++, (*n)++)
MTY_JSONArraySetItem(j, x, json_random(n));
break;
}
case 1: {
uint32_t size = MTY_GetRandomUInt(0, JSON_OBJECT_MAX);
uint32_t size = test_GetRandomUInt(0, JSON_OBJECT_MAX);
j = MTY_JSONObjCreate();

for (uint32_t x = 0; x < size && *n < JSON_ITEM_MAX; x++, (*n)++) {
Expand All @@ -71,13 +73,13 @@ static MTY_JSON *json_random(uint32_t *n)
}
case 2: {
double num = 0;
MTY_GetRandomBytes(&num, sizeof(double));
test_GetRandomBytes(&num, sizeof(double));

j = MTY_JSONNumberCreate(num);
break;
}
case 3:
j = MTY_JSONBoolCreate(MTY_GetRandomUInt(0, 2) == 1);
j = MTY_JSONBoolCreate(test_GetRandomUInt(0, 2) == 1);
break;
case 4:
j = MTY_JSONNullCreate();
Expand Down
7 changes: 3 additions & 4 deletions test/src/test/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
static bool net_websocket_echo(void)
{
uint16_t us = 0;
MTY_WebSocket *ws = MTY_WebSocketConnect("https://echo.websocket.events/",
"Origin: https://echo.websocket.events", NULL, 10000, &us);
test_cmp("MTY_WebSocketConnect", ws != NULL);
MTY_WebSocket *ws = MTY_WebSocketConnect("https://echo.websocket.events/", NULL, NULL, 10000, &us);
test_cmp_warn("MTY_WebSocketConnect", ws != NULL);
test_cmp("Upgrade Status", us == 101);

// "echo.websocket.events sponsored by Lob.com"
Expand Down Expand Up @@ -119,7 +118,7 @@ static bool net_badssl(void)
badssl_test("long-extended-subdomain-name-containing-many-letters-and-dashes.badssl.com", false);
badssl_test("longextendedsubdomainnamewithoutdashesinordertotestwordwrapping.badssl.com", false);

return result;
return true;
}

static bool net_main(void)
Expand Down
32 changes: 31 additions & 1 deletion test/src/test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,29 @@
#define RED "\x1b[91m"
#define RESET "\x1b[0m"

extern uint64_t test_seed;

static inline void test_GetRandomBytes(void *buf, size_t size)
{
uint8_t *dest = buf;
while (size) {
const size_t bytes = size < sizeof(test_seed) ? size : sizeof(test_seed);
test_seed = test_seed * 6364136223846793005ULL + 1442695040888963407ULL;
memcpy(dest, &test_seed, bytes);
dest += bytes;
size -= bytes;
}
}

static inline uint32_t test_GetRandomUInt(uint32_t lo, uint32_t hi)
{
uint32_t temp;
test_GetRandomBytes(&temp, sizeof(temp));
return lo + temp % (hi - lo);
}

#define test_print_cmp_(name, cmps, cmp, val, fmt) \
printf("[%s] %s %s" fmt "\n", name, (cmp) ? GREEN "Passed" RESET : RED "Failed" RESET, cmps, val);
printf("::%s file=%s,line=%d::%s %s" fmt "\n", (cmp) ? "notice" : "error", __FILE__, __LINE__, name, cmps, val);

#define test_cmp_(name, cmp, val, fmt) { \
bool ___CMP___ = cmp; \
Expand All @@ -20,6 +41,15 @@
#define test_cmp(name, cmp) \
test_cmp_(name, (cmp), "", "%s")

#define test_cmp_warn_(name, cmp, val, fmt) { \
bool ___CMP___ = cmp; \
test_print_cmp_(name, #cmp, ___CMP___, val, fmt); \
if (!___CMP___) return true; \
}

#define test_cmp_warn(name, cmp) \
test_cmp_warn_(name, (cmp), "", "%s")

#define test_print_cmps(name, cmp, s) { \
bool ___PCMP___ = cmp; \
test_print_cmp_(name, #cmp, ___PCMP___, s, "%s") \
Expand Down
2 changes: 1 addition & 1 deletion test/src/test/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static bool time_main(void)
test_cmp("MTY_Sleep", 100);

double diff = MTY_TimeDiff(ts, MTY_GetTime());
test_cmpf("MTY_TimeDiff", diff >= 99.0f && diff <= 115.0f, diff);
test_cmpf("MTY_TimeDiff", diff >= 99.0f, diff);

MTY_RevertTimerResolution(1);

Expand Down
Loading