Skip to content

Commit

Permalink
tests: init
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanmohd committed Jul 30, 2024
1 parent 1778164 commit 65fc6ed
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 29 deletions.
1 change: 1 addition & 0 deletions include/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void queue_thread_free(struct queue_thread *queue_thread);
void *queue_thread_entry(void *queue_thread);
int queue_pop(struct queue *queue, struct job **job);
int queue_isempty(struct job_clist *jobs);
int queue_htab_job_merge(struct job **job, struct job **htab);

#define QUEUE_H
#endif
23 changes: 23 additions & 0 deletions include/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _TEST_H
#define _TEST_H

char *__curtestname = "<none>";

#define test_run(func) \
do { \
char *orig = __curtestname; \
__curtestname = #func; \
func(); \
__curtestname = orig; \
} while (0)

#define test_assert(cond) \
do { \
if (!(cond)) { \
fprintf(stderr, "%s:%d: %s: test_assert failed: %s\n", __FILE__, __LINE__, \
__curtestname, #cond); \
abort(); \
} \
} while (0)

#endif
13 changes: 7 additions & 6 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ highs_dep = dependency('highs')
evanix_inc = include_directories('include')

if get_option('build-python')
py = import('python').find_installation()
py.extension_module(
'evanix',
'src/evanix-py.c',
install: true,
)
py = import('python').find_installation()
py.extension_module(
'evanix',
'src/evanix-py.c',
install: true,
)
endif

subdir('src')
subdir('tests')
18 changes: 8 additions & 10 deletions src/evanix.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,20 @@ static int evanix(char *expr)
{
struct queue_thread *queue_thread = NULL;
struct build_thread *build_thread = NULL;
FILE *jobsStream = NULL; /* nix-eval-jobs stdout */
FILE *jobs_stream = NULL; /* nix-eval-jobs stdout */
int ret = 0;

ret = jobs_init(&jobsStream, expr);
ret = jobs_init(&jobs_stream, expr);
if (ret < 0)
goto out_free;

ret = queue_thread_new(&queue_thread, jobsStream);
if (ret < 0) {
free(jobsStream);
ret = queue_thread_new(&queue_thread, jobs_stream);
if (ret < 0)
goto out_free;
}

ret = build_thread_new(&build_thread, queue_thread->queue);
if (ret < 0) {
free(jobsStream);
if (ret < 0)
goto out_free;
}

ret = pthread_create(&queue_thread->tid, NULL, queue_thread_entry,
queue_thread);
Expand Down Expand Up @@ -122,8 +118,10 @@ static int evanix(char *expr)
}

out_free:
fclose(jobs_stream);
queue_thread_free(queue_thread);
free(build_thread);

return ret;
}

Expand Down Expand Up @@ -153,7 +151,7 @@ int opts_read(struct evanix_opts_t *opts, char **expr, int argc, char *argv[])
switch (c) {
case 'h':
printf("%s", usage);
return -EINVAL;
return 0;
break;
case 'f':
opts->isflake = true;
Expand Down
19 changes: 10 additions & 9 deletions src/jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,16 @@ int job_read(FILE *stream, struct job **job)
goto out_free;
}

temp = cJSON_GetObjectItemCaseSensitive(root, "system");
if (!cJSON_IsString(temp)) {
ret = JOB_READ_JSON_INVAL;
goto out_free;
}
if (evanix_opts.system != NULL &&
strcmp(evanix_opts.system, temp->valuestring)) {
ret = JOB_READ_SYS_MISMATCH;
goto out_free;
if (evanix_opts.system != NULL) {
temp = cJSON_GetObjectItemCaseSensitive(root, "system");
if (!cJSON_IsString(temp)) {
ret = JOB_READ_JSON_INVAL;
goto out_free;
}
if (strcmp(evanix_opts.system, temp->valuestring)) {
ret = JOB_READ_SYS_MISMATCH;
goto out_free;
}
}

temp = cJSON_GetObjectItemCaseSensitive(root, "name");
Expand Down
2 changes: 1 addition & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ e = executable(
c_args: [f'-DNIX_EVAL_JOBS_PATH=@NIX_EVAL_JOBS_PATH@'],
)

test('evanix', e)
test('evanix', e, args: '--help')
4 changes: 1 addition & 3 deletions src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#define MAX_NIX_PKG_COUNT 200000

static int queue_push(struct queue *queue, struct job *job);
static int queue_htab_job_merge(struct job **job, struct job **htab);
static int queue_dag_isolate(struct job *job, struct job *keep_parent,
struct job_clist *jobs, struct job **htab);

Expand Down Expand Up @@ -129,7 +128,7 @@ int queue_pop(struct queue *queue, struct job **job)
* - only childrens or dependencies have parent node
* - only root node have dependencies
*/
static int queue_htab_job_merge(struct job **job, struct job **htab)
int queue_htab_job_merge(struct job **job, struct job **htab)
{
int ret;
struct job *jtab = NULL;
Expand Down Expand Up @@ -221,7 +220,6 @@ void queue_thread_free(struct queue_thread *queue_thread)
print_err("%s", strerror(errno));

free(queue_thread->queue);
fclose(queue_thread->stream);
free(queue_thread);
}

Expand Down
80 changes: 80 additions & 0 deletions tests/dag.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "util.h"
#include "queue.h"
#include "evanix.h"
#include "solver_sjf.h"
#include "test.h"

/*
* A C A C
* \ + + / = \ /
* B B B B
*/

struct evanix_opts_t evanix_opts = {
.close_unused_fd = false,
.isflake = false,
.ispipelined = true,
.isdryrun = false,
.max_build = 0,
.system = NULL,
.solver_report = false,
.check_cache_status = false,
.solver = solver_sjf,
.break_evanix = false,
};

static void test_merge()
{
FILE *stream;
struct job *job, *a, *b, *c;
struct queue_thread *qt;
int ret;

stream = fopen("../tests/dag_merge.json", "r");
test_assert(stream != NULL);

ret = queue_thread_new(&qt, stream);
if (ret < 0)
goto out_free_stram;


/* A */
ret = job_read(qt->stream, &job);
test_assert(ret == JOB_READ_SUCCESS);
ret = queue_htab_job_merge(&job, &qt->queue->htab);
test_assert(ret >= 0);
a = job;

/* B */
ret = job_read(qt->stream, &job);
test_assert(ret == JOB_READ_SUCCESS);
ret = queue_htab_job_merge(&job, &qt->queue->htab);
test_assert(ret >= 0);
b = job;

/* C */
ret = job_read(qt->stream, &job);
test_assert(ret == JOB_READ_SUCCESS);
ret = queue_htab_job_merge(&job, &qt->queue->htab);
test_assert(ret >= 0);
c = job;

ret = job_read(qt->stream, &job);
test_assert(ret == JOB_READ_EOF);

test_assert(a->deps[0] == b);
test_assert(a->deps[0] == c->deps[0]);

out_free_stram:
fclose(stream);
queue_thread_free(qt);
}

int main(void)
{
test_run(test_merge);
}
3 changes: 3 additions & 0 deletions tests/dag_merge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"name":"a","attr":"a","drvPath":"/nox/store/a.drv","inputDrvs":{"/nox/store/b.drv":["out"]},"outputs":{"out":"/nox/store/a"}}
{"attr":"b","name":"b","drvPath":"/nox/store/b.drv","inputDrvs":{},"outputs":{"out":"/nox/store/b"}}
{"name":"c","attr":"c","drvPath":"/nox/store/c.drv","inputDrvs":{"/nox/store/b.drv":["out"]},"outputs":{"out":"/nox/store/b"}}
19 changes: 19 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
dag_test = executable(
'dag_test',
[
'dag.c',
'../src/jobs.c',
'../src/util.c',
'../src/queue.c',
'../src/build.c',
'../src/jobid.c',
'../src/solver_conformity.c',
'../src/solver_highs.c',
'../src/solver_sjf.c',
],

include_directories: evanix_inc,
dependencies: [ cjson_dep, highs_dep ],
)

test('dag', dag_test)

0 comments on commit 65fc6ed

Please sign in to comment.