Skip to content
Snippets Groups Projects
Commit db8ecec4 authored by TwilCynder's avatar TwilCynder
Browse files

test architecure rework + timing test

parent 8aa5b0e2
Branches
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ all: $(BIN) ...@@ -12,7 +12,7 @@ all: $(BIN)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(OBJ_DIR) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(OBJ_DIR)
@mkdir -p $(dir $@) @mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) -c $< -o $@ $(CC) $(CPPFLAGS) -Wno-unused-parameter -c $< -o $@
$(BIN): $(OBJS) $(BIN): $(OBJS)
@printf "\033[1;33m[Linking]\033[0m \n" @printf "\033[1;33m[Linking]\033[0m \n"
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
void execute_test(Execution* execution){ void execute_test(Execution* execution){
for (int i = 0; i < execution->nb_testers; i++){ for (int i = 0; i < execution->nb_testers; i++){
Tester* tester = execution->testers + i; Tester* tester = execution->testers + i;
tester->init(&tester->state); tester->init(&tester->state, execution->context);
} }
...@@ -22,8 +22,6 @@ void execute_test(Execution* execution){ ...@@ -22,8 +22,6 @@ void execute_test(Execution* execution){
dup2(downstream[0], STDIN_FILENO); dup2(downstream[0], STDIN_FILENO);
dup2(upstream[1], STDOUT_FILENO); dup2(upstream[1], STDOUT_FILENO);
printf("Executing %s\n", execution->args[0]);
if(execv(execution->args[0], execution->args)){ if(execv(execution->args[0], execution->args)){
perror("Impossible de lancer le process mojitos"); perror("Impossible de lancer le process mojitos");
} }
...@@ -42,13 +40,14 @@ void execute_test(Execution* execution){ ...@@ -42,13 +40,14 @@ void execute_test(Execution* execution){
buffer[nread] = 0 ; buffer[nread] = 0 ;
for (int i = 0; i < execution->nb_testers; i++){ for (int i = 0; i < execution->nb_testers; i++){
Tester* tester = execution->testers + i; Tester* tester = execution->testers + i;
tester->read(tester->state, buffer, nread); tester->read(tester->state, execution->context, buffer, nread);
} }
}; };
printf("Execution results : "); printf("Execution results : \n");
for (int i = 0; i < execution->nb_testers; i++){ for (int i = 0; i < execution->nb_testers; i++){
Tester* tester = execution->testers + i; Tester* tester = execution->testers + i;
printf("| %s : %d\n", tester->name, tester->finalize(tester->state)); printf("- %s\n", tester->name);
printf("-> %s\n", tester->finalize(tester->state, execution->context) ? "Failed" : "Passed");
} }
} }
\ No newline at end of file
...@@ -8,6 +8,12 @@ typedef struct { ...@@ -8,6 +8,12 @@ typedef struct {
char*const* args; char*const* args;
Tester* testers; Tester* testers;
int nb_testers; int nb_testers;
void* context;
} Execution; } Execution;
void execute_test(Execution* execution); void execute_test(Execution* execution);
\ No newline at end of file
#define PRINT(__fmt, ...) \
printf("-> "); \
printf(__fmt, ##__VA_ARGS__);
\ No newline at end of file
#include <stdlib.h>
#include <stdio.h>
#include "../execution.h"
#include "../util.h"
#include "timing.h"
#define TOLERANCE 1
typedef struct {
char* frequency;
char* duration;
} TimingExecutionArgs;
TimingExecutionArgs timingExecutionsArgs[] = {
{.duration = "1", .frequency = "1"},
{.duration = "10", .frequency = "2"},
{.duration = "5", .frequency = "10"},
{.duration = "3", .frequency = "3"},
{.duration = "4", .frequency = "4"}
};
typedef struct {
int count;
} TimingState;
typedef struct {
int expected_result;
} TimingContext;
void timing_init(void** state, void* context){
*state = calloc(1, sizeof(TimingState));
}
void timing_reader(void* state, void* context, const char* buffer, int nread){
TimingState* tstate = state;
if (buffer[nread - 1] == '\n'){
//buffer contains a line
tstate->count++;
}
printf("Out : %s", buffer);
}
int timing_finalize(void* state, void* context){
TimingState* tstate = state;
TimingContext* tcontext = context;
PRINT("Expected %d | Got %d (tolerance : %d)\n", tcontext->expected_result, tstate->count, TOLERANCE);
int count = tstate->count;
int expected = tcontext->expected_result;
return abs(count - expected) <= TOLERANCE;
}
int calc_expected_result(TimingExecutionArgs* args){
return atoi(args->duration) * atoi(args->frequency);
}
void timing_test(){
Tester tester = {
.init = timing_init,
.read = timing_reader,
.finalize = timing_finalize,
.name = "Outputs amount"
};
TimingContext tcontext;
char* args[] = {path, "-t", 0, "-f", 0, 0};
Execution execution = {
.testers = &tester,
.nb_testers = 1,
.context = &tcontext,
.args = args,
};
for (unsigned int i = 0; i < ARRAY_LENGTH(timingExecutionsArgs); i++){
TimingExecutionArgs current_values = timingExecutionsArgs[i];
args[2] = current_values.duration;
args[4] = current_values.frequency;
((TimingContext*)execution.context)->expected_result = calc_expected_result(&current_values);
execute_test(&execution);
}
}
\ No newline at end of file
#pragma once
void timing_test();
\ No newline at end of file
...@@ -7,28 +7,29 @@ ...@@ -7,28 +7,29 @@
#include "tester.h" #include "tester.h"
#include "execution.h" #include "execution.h"
#include "executions/timing.h"
char*const e1args[] = {path, "-f", "3", "-t", "3", 0}; char*const e1args[] = {path, "-f", "3", "-t", "3", 0};
typedef struct { typedef struct {
int number; int number;
} TestState; } TestState;
void test_initialize(void** state){ void test_initialize(void** state, void* context){
TestState* newtstate = (TestState*) malloc(sizeof(TestState)); TestState* newtstate = (TestState*) malloc(sizeof(TestState));
newtstate->number = 100; newtstate->number = 100;
*state = (void*) newtstate; *state = (void*) newtstate;
} }
void test_read(void* state, const char* buffer, int nread){ void test_read(void* state, void* context, const char* buffer, int nread){
printf("Out : %s", buffer); printf("Out : %s", buffer);
if (nread < BUFFER_SIZE){ if (nread < BUFFER_SIZE){
((TestState*)state)->number += 1; ((TestState*)state)->number += 1;
} }
} }
int test_finalize(void* state){ int test_finalize(void* state, void* context){
printf("N : %d", ((TestState*)state)->number); return ((TestState*)state)->number;
return 0;
} }
char path[] = "../bin/mojitos"; char path[] = "../bin/mojitos";
...@@ -50,5 +51,7 @@ int main(int argc, char const *argv[]) ...@@ -50,5 +51,7 @@ int main(int argc, char const *argv[])
execute_test(&e1); execute_test(&e1);
timing_test();
return 0; return 0;
} }
#pragma once #pragma once
typedef void(*TesterInit)(void** state); typedef void(*TesterInit)(void** state, void* context);
typedef void(*TesterReader)(void* state, const char* buffer, int read_size); typedef void(*TesterReader)(void* state, void* context, const char* buffer, int read_size);
typedef int(*TesterFinalizer)(void* state); typedef int(*TesterFinalizer)(void* state, void* context);
typedef struct { typedef struct {
TesterInit init; TesterInit init;
......
#pragma once
#define ARRAY_LENGTH(array) sizeof(array) / sizeof(*array)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment