mirror of
https://github.com/Wessel/c-websocket-server.git
synced 2026-07-17 22:14:20 +02:00
feat: Populate repo
This commit is contained in:
57
tests/argp.test.c
Normal file
57
tests/argp.test.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <argp.h>
|
||||
|
||||
#include "../include/libtap.h"
|
||||
#include "../include/macros.h"
|
||||
|
||||
// Define the structure to store the arguments
|
||||
struct arguments {
|
||||
int port;
|
||||
int timeout;
|
||||
};
|
||||
|
||||
// Define the argument parser
|
||||
static struct argp_option options[] = {
|
||||
{"port", 'p', "PORT", 0, "Port number"},
|
||||
{"timeout", 't', "TIMEOUT", 0, "Timeout in seconds"},
|
||||
{0}
|
||||
};
|
||||
|
||||
static error_t parse_opt(int key, char* arg, struct argp_state* state) {
|
||||
struct arguments* arguments = state->input;
|
||||
switch (key) {
|
||||
case 'p':
|
||||
arguments->port = atoi(arg);
|
||||
break;
|
||||
case 't':
|
||||
arguments->timeout = atoi(arg);
|
||||
break;
|
||||
default:
|
||||
return ARGP_ERR_UNKNOWN;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct argp argp = { options, parse_opt, 0, 0 };
|
||||
|
||||
|
||||
|
||||
void run_argp_tests(int argc, char* argv[], char* env[]) {
|
||||
|
||||
printf("\033[0;35m--Tests for parsing CL args--\033[0;0m\n");
|
||||
|
||||
/* Unit tests for parsing console arguments */
|
||||
struct arguments args;
|
||||
|
||||
// Set default values for the arguments
|
||||
args.port = 8080;
|
||||
args.timeout = 10;
|
||||
dbg("defaults(Arguments { port: 8080, timeout: 10 })\n", NULL);
|
||||
|
||||
// Parse the arguments
|
||||
argp_parse(&argp, argc, argv, 0, 0, &args);
|
||||
dbg("parsed(Arguments { port: %d, timeout: %d })\n", args.port, args.timeout);
|
||||
|
||||
ok(args.port == 6060, "Arg `port` is `6060`");
|
||||
ok(args.timeout == 500, "Arg `timeout` is `500`");
|
||||
|
||||
}
|
||||
19
tests/json.test.c
Normal file
19
tests/json.test.c
Normal file
@@ -0,0 +1,19 @@
|
||||
/* Libtap for tests */
|
||||
#include "../include/libtap.h"
|
||||
|
||||
#include "../include/json.h"
|
||||
#include "../include/macros.h"
|
||||
|
||||
void run_json_tests(int argc, char* argv[], char* env[]) {
|
||||
/* Unit test for parsing JSON object*/
|
||||
printf("\033[0;35m--Tests for parsing JSON object--\033[0;0m\n");
|
||||
|
||||
char* json_string = "{ \"auth\": 1, \"payload\": { \"sensorId\": \"LDR_1\", \"sensorData\": \"55\" } }";
|
||||
Envelope envelope;
|
||||
int status_code = parse_envelope_json(json_string, &envelope);
|
||||
dbg("status_code(%d)\nEnvelope { auth: %s, payload: Message { sensorId: %s, sensorData: %s } }\n", status_code, envelope.auth, envelope.payload.sensorId, envelope.payload.sensorData);
|
||||
|
||||
ok(status_code == 0, "`parse_envelope_json` returned `json_string` casted as `Envelope`.");
|
||||
like(envelope.payload.sensorId, "LDR_1", "`envelope.payload.sensorId is the same as `json_string` (`LDR_1`)");
|
||||
like(envelope.payload.sensorData, "55", "`envelope.payload.sensorData is the same as `json_string` (`55`)");
|
||||
}
|
||||
47
tests/main.test.c
Normal file
47
tests/main.test.c
Normal file
@@ -0,0 +1,47 @@
|
||||
/* main.c */
|
||||
/*
|
||||
Project description
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "../include/tests.h"
|
||||
#include "../include/libtap.h"
|
||||
|
||||
#include "../include/macros.h"
|
||||
|
||||
int main(int argc, char* argv[], char* env[]) {
|
||||
srand(time(NULL));
|
||||
|
||||
/* Debug log: argv variables */
|
||||
dbg("argc:\t%d\n", argc);
|
||||
dbg("argv:\t", NULL);
|
||||
for (int i = 0; i < argc; i++) dbg_no_info("%s ", argv[i]);
|
||||
dbg_no_info("\n", NULL);
|
||||
|
||||
/* Debug log: env variables */
|
||||
dbg("env:\t", NULL);
|
||||
|
||||
int colors[] = { 31, 32, 33, 34, 35 };
|
||||
int colors_len = 4;
|
||||
int colors_i = 0;
|
||||
|
||||
for (int i = 0; env[i] != NULL; i++) {
|
||||
dbg_no_info("\033[0;%dm%s\033[0;0m ", colors[colors_i], env[i]);
|
||||
colors_i = colors_i == colors_len ? 0 : colors_i + 1;
|
||||
}
|
||||
dbg_no_info("\n", NULL);
|
||||
|
||||
/* Initialize unit tests */
|
||||
plan(5);
|
||||
|
||||
dbg("Running `run_json_tests`\n", NULL);
|
||||
run_json_tests(argc, argv, env);
|
||||
dbg("Running `run_argp_tests`\n", NULL);
|
||||
run_argp_tests(argc, argv, env);
|
||||
|
||||
done_testing();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user