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

fix: add checks for minimum and maximum out of bounds and add tests for the number type #3652

Merged
merged 6 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .github/workflows/ci-daily.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: gcc
run: |
gcc-8 --version
shell: bash

- name: Add or Update packages
run: sudo yarn global add [email protected]

Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/ci-weekly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: gcc
run: |
gcc-8 --version
shell: bash

- name: Setup Node.js environment
uses: actions/[email protected]
with:
Expand Down
3 changes: 2 additions & 1 deletion packages/tooling/fast-tooling-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"build": "yarn build:permutator",
"build:permutator": "node ./permutator/build.js",
"test:permutator": "node ./permutator/build.js --test && cd permutator/__tests__/ && ./test",
"start": "webpack-dev-server"
"start": "webpack-dev-server",
"test": "yarn test:permutator"
},
"devDependencies": {
"copy-webpack-plugin": "^6.0.3",
Expand Down
287 changes: 282 additions & 5 deletions packages/tooling/fast-tooling-wasm/permutator/__tests__/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,91 @@
#include "dbg.h"
#include "minunit.h"
#include <dlfcn.h>
#include <stdbool.h>
#include <math.h>
#include "../cjson/cJSON.h"
#include "../permutate.h"
#include "../type.h"

typedef int (*lib_function) (const char *data);
typedef struct PermutatedType (*lib_function) (int iteration, struct cJSON *data);
char *lib_file = "libpermutate.so";
void *lib = NULL;

int check_function(const char *func_to_run, const char *data, int expected)
int check_number_type_exact_function(
const char *func_to_run,
const int iteration,
cJSON *data,
struct PermutatedType expected
)
{
lib_function func = dlsym(lib, func_to_run);
check(func != NULL,
"Did not find %s function in the library %s: %s", func_to_run, lib_file, dlerror());

int rc = func(data);
check(rc == expected, "Function %s return %d for data: %s",
func_to_run, rc, data);
struct PermutatedType rc = func(iteration, data);
check(rc.t.number == expected.t.number, "Function %s return %d for data supplied",
func_to_run, (int) rc.t.number);

return 1;
error:
return 0;
}

int check_number_type_random_function(
const char *func_to_run,
const int iteration,
cJSON *data,
bool (*tester)(double data)
)
{
lib_function func = dlsym(lib, func_to_run);
check(func != NULL,
"Did not find %s function in the library %s: %s", func_to_run, lib_file, dlerror());

struct PermutatedType rc = func(iteration, data);
check(tester(rc.t.number), "Function %s return %d for data supplied",
func_to_run, (int) rc.t.number);

return 1;
error:
return 0;
}

bool test_minimum(double data)
{
return data >= 99;
}

bool test_exclusive_minimum(double data)
{
return data > 99;
}

bool test_maximum(double data)
{
return data <= 1;
}

bool test_exclusive_maximum(double data)
{
return data < 1;
}

bool test_minimum_and_maximum(double data)
{
return data >= 6 && data <= 7;
}

bool test_exclusive_minimum_and_maximum(double data)
{
return data > 6 && data < 8;
}

bool test_multiple_of(double data)
{
return fmod(data,10) == 0;
}

char *test_dlopen()
{
lib = dlopen(lib_file, RTLD_NOW);
Expand All @@ -31,11 +95,224 @@ char *test_dlopen()
return NULL;
}

/**
* Tests the function called when JSON schema is type "number"
*/
char *test_number_type()
janechu marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* Default provided
*/
cJSON *defaultConfig = cJSON_Parse("{\"default\": 5}");
struct PermutatedType defaultConfigExpected = {0};
defaultConfigExpected.t.number = 5;

mu_assert(
check_number_type_exact_function(
"permutate_number",
0,
defaultConfig,
defaultConfigExpected
),
"Failed: should have supplied a default"
);

/**
* Enum provided
*/
cJSON *enumConfig = cJSON_Parse("{\"enum\": [8,4,1]}");
struct PermutatedType enumConfigExpected0 = {0};
enumConfigExpected0.t.number = 8;
struct PermutatedType enumConfigExpected1 = {0};
enumConfigExpected1.t.number = 4;
struct PermutatedType enumConfigExpected2 = {0};
enumConfigExpected2.t.number = 1;
struct PermutatedType enumConfigExpected3 = {0};
enumConfigExpected3.t.number = 8;

mu_assert(
check_number_type_exact_function(
"permutate_number",
0,
enumConfig,
enumConfigExpected0
),
"Failed: should have supplied a value from an enum"
);
mu_assert(
check_number_type_exact_function(
"permutate_number",
1,
enumConfig,
enumConfigExpected1
),
"Failed: should have supplied a value from an enum"
);
mu_assert(
check_number_type_exact_function(
"permutate_number",
2,
enumConfig,
enumConfigExpected2
),
"Failed: should have supplied a value from an enum"
);
mu_assert(
check_number_type_exact_function(
"permutate_number",
3,
enumConfig,
enumConfigExpected3
),
"Failed: should have supplied a value from an enum"
);

/**
* Examples provided
*/
cJSON *examplesConfig = cJSON_Parse("{\"examples\": [9,40,13]}");
struct PermutatedType examplesConfigExpected0 = {0};
examplesConfigExpected0.t.number = 9;
struct PermutatedType examplesConfigExpected1 = {0};
examplesConfigExpected1.t.number = 40;
struct PermutatedType examplesConfigExpected2 = {0};
examplesConfigExpected2.t.number = 13;

mu_assert(
check_number_type_exact_function(
"permutate_number",
0,
examplesConfig,
examplesConfigExpected0
),
"Failed: should have supplied a value from an enum"
);
mu_assert(
check_number_type_exact_function(
"permutate_number",
1,
examplesConfig,
examplesConfigExpected1
),
"Failed: should have supplied a value from an enum"
);
mu_assert(
check_number_type_exact_function(
"permutate_number",
2,
examplesConfig,
examplesConfigExpected2
),
"Failed: should have supplied a value from an enum"
);

/**
* Minimum provided
*/
cJSON *minimumConfig = cJSON_Parse("{\"minimum\": 99}");
mu_assert(
check_number_type_random_function(
"permutate_number",
0,
minimumConfig,
test_minimum
),
"Failed: should have supplied a random value above a minimum"
);

/**
* Maximum provided
*/
cJSON *maximumConfig = cJSON_Parse("{\"maximum\": 1}");
mu_assert(
check_number_type_random_function(
"permutate_number",
0,
maximumConfig,
test_maximum
),
"Failed: should have supplied a random value below a maximum"
);

/**
* Minimum & Maximum provided
*/
cJSON *minimumAndMaximumConfig = cJSON_Parse("{\"minimum\": 6, \"maximum\": 7}");
mu_assert(
check_number_type_random_function(
"permutate_number",
0,
minimumAndMaximumConfig,
test_minimum_and_maximum
),
"Failed: should have supplied a random value between a minimum and a maximum"
);

/**
* Exclusive minimum provided
*/
cJSON *exclusiveMinimumConfig = cJSON_Parse("{\"exclusiveMinimum\": 99}");
mu_assert(
check_number_type_random_function(
"permutate_number",
0,
exclusiveMinimumConfig,
test_exclusive_minimum
),
"Failed: should have supplied a random value above an exclusive minimum"
);

/**
* Exclusive maximum provided
*/
cJSON *exclusiveMaximumConfig = cJSON_Parse("{\"exclusiveMaximum\": 1}");
mu_assert(
check_number_type_random_function(
"permutate_number",
0,
exclusiveMaximumConfig,
test_exclusive_maximum
),
"Failed: should have supplied a random value below an exclusive maximum"
);

/**
* Exclusive minimum & exclusive maximum provided
*/
cJSON *exclusiveMinimumAndMaximumConfig = cJSON_Parse("{\"exclusiveMinimum\": 6, \"exclusiveMaximum\": 8}");
mu_assert(
check_number_type_random_function(
"permutate_number",
0,
exclusiveMinimumAndMaximumConfig,
test_exclusive_minimum_and_maximum
),
"Failed: should have supplied a random value between an exclusive minimum and an exclusive maximum"
);

/**
* Multiple of provided
*/
cJSON *multipleOfConfig = cJSON_Parse("{\"multipleOf\": 10}");
mu_assert(
check_number_type_random_function(
"permutate_number",
0,
multipleOfConfig,
test_multiple_of
),
"Failed: should have supplied a random value that was a multiple of"
);

return NULL;
}

char *all_tests()
{
mu_suite_start();

mu_run_test(test_dlopen);
mu_run_test(test_number_type);

return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tooling/fast-tooling-wasm/permutator/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const emccFiles = ["wasm.c", "permutate.c"].concat(commonFiles).join(" ");
/**
* Test file needed for compilation
*/
const staticTestFilesCompile = `gcc -Wall -L. "-Wl,-rpath,." -o test test.c -ldl -lpermutate -o test`;
const staticTestFilesCompile = `gcc -Wall -L. "-Wl,-rpath,." -o test test.c -ldl -lpermutate -lm -o test`;
const sharedLibSetup = `gcc -Wall -c -fPIC permutate.c -o libpermutate.o`;
const sharedLibCompile = `gcc -Wall -shared -fPIC -o libpermutate.so ${commonFiles.join(
" "
Expand Down
Loading