#243 unit test
This commit is contained in:
@@ -4,7 +4,9 @@ LDFLAGS =
|
||||
|
||||
tests = wiringpi_test1_sysfs wiringpi_test2_sysfs wiringpi_test3_device_wpi wiringpi_test4_device_phys wiringpi_test5_default wiringpi_test6_isr wiringpi_test7_version
|
||||
|
||||
all: $(tests)
|
||||
xotests = wiringpi_xotest_test1_spi
|
||||
|
||||
all: $(tests) $(xotests)
|
||||
|
||||
wiringpi_test1_sysfs:
|
||||
${CC} ${CFLAGS} wiringpi_test1_sysfs.c -o wiringpi_test1_sysfs -lwiringPi
|
||||
@@ -27,12 +29,42 @@ wiringpi_test6_isr:
|
||||
wiringpi_test7_version:
|
||||
${CC} ${CFLAGS} wiringpi_test7_version.c -o wiringpi_test7_version -lwiringPi
|
||||
|
||||
wiringpi_xotest_test1_spi:
|
||||
${CC} ${CFLAGS} wiringpi_xotest_test1_spi.c -o wiringpi_xotest_test1_spi -lwiringPi
|
||||
|
||||
test:
|
||||
@error_state=false ; \
|
||||
for t in $(tests) ; do \
|
||||
echo === unit test: $${t} === ; \
|
||||
time ./$${t}; \
|
||||
if [ $$? -ne 0 ]; then \
|
||||
error_state=true ; \
|
||||
fi ; \
|
||||
echo ; echo ; \
|
||||
done ; \
|
||||
if [ "$$error_state" = true ]; then \
|
||||
echo "STD TEST FAILED"; \
|
||||
else \
|
||||
echo "STD TEST SUCCESS"; \
|
||||
fi
|
||||
|
||||
xotest:
|
||||
@error_state=false ; \
|
||||
for t in $(xotests) ; do \
|
||||
echo === unit test: $${t} === ; \
|
||||
time ./$${t} ; \
|
||||
if [ $$? -ne 0 ]; then \
|
||||
error_state=true ; \
|
||||
fi ; \
|
||||
echo ; echo ; \
|
||||
done
|
||||
if [ "$$error_state" = true ]; then \
|
||||
echo "XO TEST FAILED"; \
|
||||
else \
|
||||
echo "XO TEST SUCCESS"; \
|
||||
fi
|
||||
|
||||
clean:
|
||||
for t in $(tests) ; do rm -fv $${t} ; done
|
||||
for t in $(tests) $(xotests) ; do \
|
||||
rm -fv $${t} ; \
|
||||
done
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
// Compile: gcc -Wall wiringpi_test1_device.c -o wiringpi_test1_device -lwiringPi
|
||||
|
||||
#include "wpi_test.h"
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
@@ -52,6 +50,6 @@ int main (void) {
|
||||
//Error wrong direction - only for fun
|
||||
digitalWrite(GPIO, LOW);
|
||||
|
||||
return(EXIT_SUCCESS);
|
||||
return UnitTestState();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
// Compile: gcc -Wall wiringpi_test2_device.c -o wiringpi_test2_device -lwiringPi
|
||||
|
||||
#include "wpi_test.h"
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
@@ -50,5 +48,5 @@ int main (void) {
|
||||
CheckGPIO(GPIO, GPIOIN, LOW);
|
||||
delayMicroseconds(600000);
|
||||
|
||||
return(EXIT_SUCCESS);
|
||||
return UnitTestState();
|
||||
}
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
// Compile: gcc -Wall wiringpi_test3_device.c -o wiringpi_test3_device -lwiringPi
|
||||
|
||||
#include "wpi_test.h"
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
@@ -50,5 +48,5 @@ int main (void) {
|
||||
CheckGPIO(GPIO, GPIOIN, LOW);
|
||||
delayMicroseconds(600000);
|
||||
|
||||
return(EXIT_SUCCESS);
|
||||
return UnitTestState();
|
||||
}
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
// Compile: gcc -Wall wiringpi_test4_device.c -o wiringpi_test4_device -lwiringPi
|
||||
|
||||
#include "wpi_test.h"
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
@@ -50,5 +48,5 @@ int main (void) {
|
||||
CheckGPIO(GPIO, GPIOIN, LOW);
|
||||
delayMicroseconds(600000);
|
||||
|
||||
return(EXIT_SUCCESS);
|
||||
return UnitTestState();
|
||||
}
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
// Compile: gcc -Wall wiringpi_test1_device.c -o wiringpi_test1_device -lwiringPi
|
||||
|
||||
#include "wpi_test.h"
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
@@ -52,6 +50,6 @@ int main (void) {
|
||||
//Error wrong direction - only for fun
|
||||
digitalWrite(GPIO, LOW);
|
||||
|
||||
return(EXIT_SUCCESS);
|
||||
return UnitTestState();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include "wpi_test.h"
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
@@ -166,5 +165,5 @@ int main (void) {
|
||||
}
|
||||
pinMode(OUTpin, INPUT);
|
||||
|
||||
return 0 ;
|
||||
return UnitTestState();
|
||||
}
|
||||
|
||||
@@ -8,4 +8,6 @@ int main (void) {
|
||||
|
||||
CheckSame("version major", major, VERSION_MAJOR);
|
||||
CheckSame("version minor", minor, VERSION_MINOR);
|
||||
|
||||
return UnitTestState();
|
||||
}
|
||||
178
wiringPi/test/wiringpi_xotest_test1_spi.c
Normal file
178
wiringPi/test/wiringpi_xotest_test1_spi.c
Normal file
@@ -0,0 +1,178 @@
|
||||
// WiringPi test program: SPI functions (need XO hardware)
|
||||
// Compile: gcc -Wall wiringpi_xotest_test1_spi.c -o wiringpi_xotest_test1_spi -lwiringPi
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include "wpi_test.h"
|
||||
#include <wiringPiSPI.h>
|
||||
|
||||
#define TRUE (1==1)
|
||||
#define FALSE (!TRUE)
|
||||
#define CHAN_CONFIG_SINGLE 8
|
||||
#define CHAN_CONFIG_DIFF 0
|
||||
|
||||
const float fRefVoltage = 3.3f;
|
||||
const float fResolution = 4096; //12-Bit
|
||||
const int spiChannel = 1;
|
||||
const int spiSpeed = 1000000; // MHz
|
||||
|
||||
|
||||
|
||||
int AnalogRead(int spiChannel, int analogChannel, int* returnvalue) {
|
||||
if (analogChannel<0 || analogChannel>1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char spiData[3];
|
||||
unsigned char chanBits;
|
||||
|
||||
if (analogChannel == 0) {
|
||||
chanBits = 0b11010000;
|
||||
} else {
|
||||
chanBits = 0b11110000;
|
||||
}
|
||||
spiData[0] = chanBits;
|
||||
spiData[1] = 0;
|
||||
spiData[2] = 0;
|
||||
*returnvalue = wiringPiSPIxDataRW(0, spiChannel, spiData, 3);
|
||||
return ((spiData [0] << 9) | (spiData [1] << 1) | (spiData[2] >> 7)) & 0xFFF;
|
||||
}
|
||||
|
||||
void checkVoltage(float expect, const char* szexpect) {
|
||||
int returnvalue;
|
||||
|
||||
//int CH0 = AnalogRead(spiChannel, 0, &returnvalue);
|
||||
int CH1 = AnalogRead(spiChannel, 1, &returnvalue);
|
||||
//float value0 = CH0 * fRefVoltage / fResolution;
|
||||
float value1 = CH1 * fRefVoltage / fResolution;
|
||||
CheckSameFloat(szexpect, value1, expect);
|
||||
delayMicroseconds(300);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv []){
|
||||
|
||||
const int GPIOIn = 29;
|
||||
int hSPI;
|
||||
//int CH0;
|
||||
int CH1;
|
||||
int major, minor;
|
||||
|
||||
wiringPiVersion(&major, &minor);
|
||||
printf("Testing SPI functions with WiringPi %d.%d\n",major, minor);
|
||||
printf("------------------------------------------\n\n");
|
||||
|
||||
wiringPiSetup();
|
||||
|
||||
if ((hSPI = wiringPiSPISetup (spiChannel, spiSpeed)) < 0) {
|
||||
FailAndExitWithErrno("wiringPiSPISetup", hSPI);
|
||||
}
|
||||
|
||||
int hSPIOld=hSPI;
|
||||
//printf("\nSPI fd = %d\n call close now\n", hSPI);
|
||||
int ret = wiringPiSPIClose(spiChannel);
|
||||
if (ret!=0) {
|
||||
FailAndExitWithErrno("wiringPiSPIClose", ret);
|
||||
}
|
||||
|
||||
if ((hSPI = wiringPiSPIxSetupMode(0, spiChannel, spiSpeed, 0)) < 0) {
|
||||
FailAndExitWithErrno("wiringPiSPIxSetup", hSPI);
|
||||
}
|
||||
|
||||
CheckSame("SPISetup, Close and SPIxSetup handle", hSPI, hSPIOld);
|
||||
|
||||
int returnvalue;
|
||||
//CH0 = AnalogRead(spiChannel, 0, &returnvalue);
|
||||
CH1 = AnalogRead(spiChannel, 1, &returnvalue);
|
||||
CheckSame("SPI reading ioctl result (byte count) ", returnvalue, 3);
|
||||
//float value0 = CH0 * fRefVoltage / fResolution;
|
||||
//float value1 = CH1 * fRefVoltage / fResolution;
|
||||
|
||||
pinMode(21, OUTPUT);
|
||||
pinMode(22, INPUT);
|
||||
pinMode(24, INPUT);
|
||||
pinMode(25, INPUT);
|
||||
pinMode(27, INPUT);
|
||||
pinMode(28, INPUT);
|
||||
pinMode(GPIOIn, INPUT);
|
||||
|
||||
|
||||
digitalWriteEx(21, GPIOIn, LOW);
|
||||
checkVoltage(0.1f, "Analog value 1xLow");
|
||||
checkVoltage(0.1f, "Analog value 1xLow");
|
||||
|
||||
digitalWriteEx(21, GPIOIn, HIGH);
|
||||
checkVoltage(3.0f, "Analog value 1xHigh");
|
||||
|
||||
pinMode(22, OUTPUT);
|
||||
digitalWriteEx(22, -1, LOW);
|
||||
checkVoltage(1.55f, "Analog value Half (1H/1L)");
|
||||
|
||||
digitalWriteEx(22, GPIOIn, HIGH);
|
||||
checkVoltage(3.1f, "Analog value 2xHigh");
|
||||
|
||||
pinMode(24, OUTPUT);
|
||||
digitalWriteEx(24, GPIOIn, HIGH);
|
||||
checkVoltage(3.2f, "Analog value 3xHigh");
|
||||
|
||||
digitalWriteEx(24, -1, LOW);
|
||||
checkVoltage(2.2f, "Analog value 2xHigh/1xLow");
|
||||
checkVoltage(2.2f, "Analog value 2xHigh/1xLow");
|
||||
|
||||
pinMode(25, OUTPUT);
|
||||
digitalWriteEx(25, GPIOIn, HIGH);
|
||||
checkVoltage(2.475f, "Analog value 3xHigh/1xLow");
|
||||
|
||||
digitalWriteEx(25, -1, LOW);
|
||||
checkVoltage(1.65f, "Analog value Half (2H/2L)");
|
||||
|
||||
pinMode(27, OUTPUT);
|
||||
digitalWriteEx(27, GPIOIn, HIGH);
|
||||
checkVoltage(1.98f, "Analog value 3xHigh/2xLow");
|
||||
|
||||
digitalWriteEx(27, -1, LOW);
|
||||
checkVoltage(1.32f, "Analog value Half (2H/3L)");
|
||||
|
||||
pinMode(28, OUTPUT);
|
||||
digitalWriteEx(28, GPIOIn, LOW);
|
||||
checkVoltage(1.100f, "Analog value 2xHigh/4xLow");
|
||||
|
||||
digitalWriteEx(28, GPIOIn, HIGH);
|
||||
checkVoltage(1.65f, "Analog value Half (3H/3L)");
|
||||
|
||||
digitalWriteEx(27, GPIOIn, HIGH);
|
||||
checkVoltage(2.2f, "Analog value 4xHigh/2xLow");
|
||||
|
||||
digitalWriteEx(25, GPIOIn, HIGH);
|
||||
checkVoltage(2.75f, "Analog value 5xHigh/1xLow");
|
||||
|
||||
digitalWriteEx(24, GPIOIn, HIGH);
|
||||
checkVoltage(3.3f, "Analog value 6xHigh");
|
||||
|
||||
CH1 = AnalogRead(3, 1, &returnvalue);
|
||||
CheckSame("\nReading Wrong channel 3 result ", CH1, 0);
|
||||
CheckSame("\nReading Wrong channel 3 ioctl result ", returnvalue, -EINVAL);
|
||||
CH1 = AnalogRead(2, 1, &returnvalue);
|
||||
CheckSame("\nReading Wrong channel 2 result ", CH1, 0);
|
||||
CheckSame("\nReading Wrong channel 3 ioctl result ", returnvalue, -EBADF);
|
||||
|
||||
pinMode(22, INPUT);
|
||||
pinMode(21, INPUT);
|
||||
pinMode(24, INPUT);
|
||||
pinMode(25, INPUT);
|
||||
pinMode(27, INPUT);
|
||||
pinMode(28, INPUT);
|
||||
|
||||
ret = wiringPiSPIxClose(0, spiChannel);
|
||||
CheckSame("wiringPiSPIxClose result", ret, 0);
|
||||
if (ret!=0) {
|
||||
FailAndExitWithErrno("wiringPiSPIxClose", ret);
|
||||
}
|
||||
ret = wiringPiSPIxGetFd(0, spiChannel);
|
||||
CheckSame("Fd after close", ret, -1);
|
||||
|
||||
return UnitTestState();
|
||||
}
|
||||
|
||||
@@ -1,48 +1,100 @@
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#define COLORDEF "\x1B[0m"
|
||||
#define COLORRED "\x1B[31m"
|
||||
#define COLORGRN "\x1B[32m"
|
||||
#define BOLD "\x1B[1m"
|
||||
#define FINALCOLRED "\x1B[7;49;91m"
|
||||
#define FINALCOLGRN "\x1B[7;49;32m"
|
||||
|
||||
|
||||
unsigned int globalError = 0;
|
||||
|
||||
void CheckGPIO(int GPIO, int GPIOIN, int out) {
|
||||
int in = digitalRead(GPIOIN);
|
||||
int read = digitalRead(GPIO);
|
||||
|
||||
int pass = 0;
|
||||
if (out==in && in==read) {
|
||||
pass = 1;
|
||||
}
|
||||
if (pass) {
|
||||
printf("GPIO%d = %d (GPIO%d = %d) -> %spassed%s\n", GPIOIN, in, GPIO, read, COLORGRN, COLORDEF );
|
||||
} else {
|
||||
printf("GPIO%d = %d (GPIO%d = %d) -> %sfailed%s\n", GPIOIN, in, GPIO, read, COLORRED, COLORDEF );
|
||||
}
|
||||
int in = out;
|
||||
if (GPIOIN>=0) {
|
||||
in = digitalRead(GPIOIN);
|
||||
}
|
||||
int readback = digitalRead(GPIO);
|
||||
|
||||
int pass = 0;
|
||||
if (out==readback && in==out) {
|
||||
pass = 1;
|
||||
}
|
||||
|
||||
if (GPIOIN>=0) {
|
||||
printf("set GPIO%02d = %d (readback %d), in GPIO%02d = %d ", GPIO, out, readback, GPIOIN, in);
|
||||
} else {
|
||||
printf("set GPIO%02d = %d (readback %d) ", GPIO, out, readback);
|
||||
}
|
||||
|
||||
if (pass) {
|
||||
printf("-> %spassed%s\n", COLORGRN, COLORDEF );
|
||||
} else {
|
||||
globalError=1;
|
||||
printf("-> %sfailed%s\n", COLORRED, COLORDEF );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void digitalWriteEx(int GPIO, int GPIOIN, int mode) {
|
||||
digitalWrite(GPIO, mode);
|
||||
printf("out = %d ", mode);
|
||||
delayMicroseconds(5000);
|
||||
CheckGPIO(GPIO, GPIOIN, mode);
|
||||
digitalWrite(GPIO, mode);
|
||||
delayMicroseconds(5000);
|
||||
CheckGPIO(GPIO, GPIOIN, mode);
|
||||
}
|
||||
|
||||
|
||||
void pullUpDnControlEx (int GPIO, int GPIOIN, int mode) {
|
||||
pullUpDnControl (GPIO, mode);
|
||||
int out = mode==PUD_UP ? 1:0;
|
||||
printf("in = %4s ", mode==PUD_UP ? "up":"down");
|
||||
delayMicroseconds(5000);
|
||||
CheckGPIO(GPIO, GPIOIN, out);
|
||||
pullUpDnControl (GPIO, mode);
|
||||
int out = mode==PUD_UP ? 1:0;
|
||||
printf("in = %4s ", mode==PUD_UP ? "up":"down");
|
||||
delayMicroseconds(5000);
|
||||
CheckGPIO(GPIO, GPIOIN, out);
|
||||
}
|
||||
|
||||
|
||||
void CheckSame(const char* msg, int value, int expect) {
|
||||
if (value==expect) {
|
||||
printf("%s -> %spassed%s\n", msg, COLORGRN, COLORDEF );
|
||||
} else {
|
||||
printf("%s -> %sfailed%s\n", msg, COLORRED, COLORDEF );
|
||||
}
|
||||
if (value==expect) {
|
||||
printf("%39s (% 3d==% 3d) -> %spassed%s\n", msg, value, expect, COLORGRN, COLORDEF);
|
||||
} else {
|
||||
printf("%39s (% 3d<>% 3d) -> %sfailed%s\n", msg, value, expect, COLORRED, COLORDEF);
|
||||
globalError=1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CheckSameFloat(const char* msg, float value, float expect) {
|
||||
if (fabs(value-expect)<0.08) {
|
||||
printf("%35s (%.3f==%.3f) -> %spassed%s \n", msg, value, expect, COLORGRN, COLORDEF);
|
||||
} else {
|
||||
printf("%35s (%.3f<>%.3f) -> %sfailed%s \n" , msg, value, expect, COLORRED, COLORDEF);
|
||||
globalError=1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int UnitTestState() {
|
||||
printf("\n\nUNIT TEST STATE: ");
|
||||
if (globalError) {
|
||||
printf(" %sFAILED%s\n\n", FINALCOLRED, COLORDEF);
|
||||
return EXIT_FAILURE;
|
||||
} else {
|
||||
printf(" %sPASSED%s\n\n", FINALCOLGRN, COLORDEF);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void FailAndExitWithErrno(const char* msg, int ret) {
|
||||
printf("%s (Return=%d, Err: %s) -> %sfailed%s \n" , msg, ret, strerror(errno), COLORRED, COLORDEF);
|
||||
globalError=1;
|
||||
exit(UnitTestState());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user