aboutsummaryrefslogtreecommitdiff
path: root/src/test.c
blob: 37a3b641fd01c41713f7012478c60e2e9a288dec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "k8055.h"

static int port = 0;


int test_all_analog(k8055_device* device) {
	
	for (int i = 0; i < 256; ++i)
		if (k8055_set_all_analog(device, i, i) !=0 )
			return -1;
	return 0;
}

int test_all_digital(k8055_device* device) {
	for (int i = 0; i < 256; ++i)
		if (k8055_set_all_digital(device, i) !=0 )
			return -1;
	return 0;
}

int test_analog(k8055_device* device) {
	struct timespec reqtime;
        reqtime.tv_sec = 0;
        reqtime.tv_nsec = 500000000;

	if (k8055_set_analog(device, 0, 0) != 0) return -1;
	if (k8055_set_analog(device, 1, 0) != 0) return -1;
        nanosleep(&reqtime, NULL);
	if (k8055_set_analog(device, 0, 255) != 0) return -1;
	if (k8055_set_analog(device, 1, 255) != 0) return -1;
        nanosleep(&reqtime, NULL);
	return 0;
}

int test_digital(k8055_device* device) {
	struct timespec reqtime;
        reqtime.tv_sec = 0;
        reqtime.tv_nsec = 500000000;

	for (int i = 0; i < 8; ++i)
		if (k8055_set_digital(device, i, false) != 0) return -1;
        nanosleep(&reqtime, NULL);
	for (int i = 0; i < 8; ++i)
		if (k8055_set_digital(device, i, true) != 0) return -1;
        nanosleep(&reqtime, NULL);
	return 0;
}

int test_get_all_input(k8055_device* device) {
	if (k8055_get_all_input(device, NULL, NULL, NULL, NULL, NULL, false) != 0) return -1;
	if (k8055_get_all_input(device, NULL, NULL, NULL, NULL, NULL, true) != 0) return -1;
	return 0;
}

int test_get_all_output(k8055_device* device) {
	unsigned int iseed = (unsigned int)time(NULL);
	srand(iseed);
	
	
	int a0 = rand() % 256;
	int a1 = rand() % 256;
	int d = rand() % 256;
	
	if (k8055_set_all_analog(device, a0, a1) != 0) return -1;
	if (k8055_set_all_digital(device, d) != 0) return -1;
	
	int ga0;
	int ga1;
	int gd;
	k8055_get_all_output(device, &gd, &ga0, &ga1, NULL, NULL);

	if ((a0 != ga0) || (a1 != ga1) || (d != gd)) return -1;
	return 0;
}

int run_test(const char* name, int (*f)(k8055_device*), k8055_device* device) {
	puts(name);
	int result = f(device);
	if (result == 0) puts("= success =");
	else puts("= FAILED =");
	puts("");
	return result;
}

int run_all() {
	k8055_debug(true);
	k8055_device* device = NULL;
	
	size_t n = 6;
	char* names[] = {
		"= write all analog =",
		"= write all digital =",
		"= write analog =",
		"= write digital =",
		"= read input =",
		"= read output ="
	};
	
	int (*tests[])(k8055_device*) = {
		test_all_analog,
		test_all_digital,
		test_analog,
		test_digital,
		test_get_all_input,
		test_get_all_output
	};
	


	printf("= open k8055 on port %i =\n", port);
	if (k8055_open_device(port, &device) == 0) {
		puts("= success =");
		puts("");
	} else {
		puts("= FAILED =\n");
		puts("");
		return -1;
	}

	for (int j = 0; j < n; ++j)
		run_test(names[j], tests[j], device);

	printf("= reopen k8055 on port %i =\n", port);
	k8055_close_device(device);
	if (k8055_open_device(port, &device) == 0) {
	puts("= success =");
		puts("");
	} else {
		puts("= FAILED =\n");
		puts("");
		return -1;
	}

	for (int j = 0; j < n; ++j)
		run_test(names[j], tests[j], device);

	
	puts("turning everything off");
	k8055_set_all_analog(device, 0, 0);
	k8055_set_all_digital(device, 0);
	k8055_close_device(device);

	return 0;	
}

int main(int argc, char *argv[]) {
	if (argc <= 1) port = 0;
	else port = atoi(argv[1]);

	int r = run_all();
	puts("");
	if (r == 0) puts("all tests completed successfully");
	else puts("some tests FAILED");
	return r;
}