From 40f91354120d8f91aabd041ba04cd42d25da83b2 Mon Sep 17 00:00:00 2001 From: Stefan Andritoiu Date: Wed, 13 Jan 2016 13:38:23 +0200 Subject: [PATCH] java: Added sanity checks and integrated them in CMake. Updated sample names and sample mapping. Signed-off-by: Stefan Andritoiu Signed-off-by: Mihai Tudor Panu --- CMakeLists.txt | 9 +++++ doxy/samples.mapping.txt | 55 ++------------------------ examples/java/CMakeLists.txt | 4 +- examples/java/Joystick12Sample.java | 44 +++++++++++++++++++++ examples/java/LoLSample.java | 47 ++++++++++++++++++++++ src/CMakeLists.txt | 26 ++++++++++--- src/javaswig_blacklist | 1 + src/nodeswig_blacklist | 0 src/pythonswig_blacklist | 0 tests/CMakeLists.txt | 17 ++++++++ tests/check_autoloadlibrary.py | 44 +++++++++++++++++++++ tests/check_clean.py | 35 +++++++++++++++++ tests/check_consistency.py | 60 +++++++++++++++++++++++++++++ tests/check_samplenames.py | 53 +++++++++++++++++++++++++ 14 files changed, 335 insertions(+), 60 deletions(-) create mode 100644 examples/java/Joystick12Sample.java create mode 100644 examples/java/LoLSample.java create mode 100644 src/javaswig_blacklist create mode 100644 src/nodeswig_blacklist create mode 100644 src/pythonswig_blacklist create mode 100644 tests/CMakeLists.txt create mode 100755 tests/check_autoloadlibrary.py create mode 100755 tests/check_clean.py create mode 100755 tests/check_consistency.py create mode 100755 tests/check_samplenames.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 82fb9065..12d5e425 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ option (BUILDJAVAEXAMPLES "Build java example jars" OFF) option (BUILDSWIGJAVA "Build swig java modules" OFF) option (IPK "Generate IPK using CPack" OFF) option (RPM "Generate RPM using CPack" OFF) +option (BUILDTESTS "Generate check-ups for upm" OFF) # Find swig if (BUILDSWIG) @@ -208,3 +209,11 @@ endif() if(BUILDJAVAEXAMPLES) add_subdirectory (examples/java) endif() + +if(BUILDTESTS) + find_package (PythonInterp REQUIRED) + if (${PYTHONINTERP_FOUND}) + enable_testing () + add_subdirectory (tests) + endif () +endif() diff --git a/doxy/samples.mapping.txt b/doxy/samples.mapping.txt index 827d4196..db7c4c7c 100644 --- a/doxy/samples.mapping.txt +++ b/doxy/samples.mapping.txt @@ -1,40 +1,21 @@ # Mapping C++ sample files to Java sample files a110x-intr.cxx A110X_intrSample.java a110x.cxx A110XSample.java -ad8232.cxx AD8232Sample.java adc121c021.cxx ADC121C021Sample.java -adis16448.cxx ADIS16448Sample.java -adxl335.cxx ADXL335Sample.java adxl345.cxx Adxl345Sample.java -ak8975.cxx AK8975Sample.java -am2315.cxx AM2315Sample.java -apds9002.cxx APDS9002Sample.java biss0001.cxx BISS0001Sample.java bmpx8x.cxx BMPX8XSample.java buzzer-sound.cxx Buzzer_soundSample.java cjq4435.cxx CJQ4435Sample.java ds1307.cxx DS1307Sample.java -eboled.cxx EBOLEDSample.java -ecs1030.cxx ECS1030Sample.java enc03r.cxx ENC03RSample.java es08a.cxx ES08ASample.java -flex.cxx FlexSample.java -gp2y0a.cxx GP2Y0ASample.java grovebutton.cxx GroveButtonSample.java -grovecircularled.cxx GroveCircularLEDSample.java -grovecollision.cxx GroveCollisionSample.java groveehr.cxx GroveEHRSample.java -groveeldriver.cxx GroveElDriverSample.java -groveelectromagnet.cxx GroveElectromagnetSample.java -groveemg.cxx GroveEMGSample.java -grovegprs.cxx GroveGPRSSample.java -grovegsr.cxx GroveGSRSample.java groveled.cxx GroveLEDSample.java grovelinefinder.cxx GroveLineFinderSample.java grovelight.cxx GroveLightSample.java -groveloudness.cxx GroveLoudnessSample.java grovemoisture.cxx GroveMoistureSample.java -groveo2.cxx GroveO2Sample.java groverelay.cxx GroveRelaySample.java groverotary.cxx GroveRotarySample.java grovescam.cxx GROVESCAMSample.java @@ -48,75 +29,45 @@ h3lis331dl.cxx H3LIS331DLSample.java hcsr04.cxx HCSR04Sample.java hm11.cxx HM11Sample.java hmc5883l.cxx Hmc5883lSample.java -hp20x.cxx HP20XSample.java -ht9170.cxx HT9170Sample.java htu21d.cxx HTU21DSample.java -hx711.cxx HX711Sample.java -hyld9767.cxx HYLD9767Sample.java -ina132.cxx INA132Sample.java itg3200.cxx Itg3200Sample.java jhd1313m1-lcd.cxx Jhd1313m1_lcdSample.java -joystick12-example.cxx Joystick12_exampleSample.java -l298-stepper.cxx L298_stepperSample.java -l298.cxx L298Sample.java -lcm1602-lcd.cxx Lcm1602_lcdSample.java +joystick12.cxx Joystick12Sample.java +lcm1602-i2c.cxx Lcm1602_i2cSample.java ldt0028.cxx LDT0028Sample.java -lol-example.cxx LoL_exampleSample.java -lpd8806.cxx LPD8806Sample.java +lol.cxx LoLSample.java lsm303.cxx LSM303Sample.java -lsm9ds0.cxx LSM9DS0Sample.java m24lr64e.cxx M24LR64ESample.java -max31723.cxx MAX31723Sample.java -max31855.cxx MAX31855Sample.java max44000.cxx MAX44000Sample.java -max5487.cxx MAX5487Sample.java -maxds3231m.cxx MAXDS3231MSample.java -mg811.cxx MG811Sample.java mic.cxx MicrophoneSample.java -mlx90614.cxx MLX90614Sample.java mma7455.cxx MMA7455Sample.java mma7660.cxx MMA7660Sample.java mpl3115a2.cxx MPL3115A2Sample.java mpr121.cxx MPR121Sample.java -mpu60x0.cxx MPU60X0Sample.java mpu9150.cxx MPU9150Sample.java -mpu9250.cxx MPU9250Sample.java mq2.cxx MQ2Sample.java -mq3.cxx MQ3Sample.java mq303a.cxx MQ303ASample.java mq5.cxx MQ5Sample.java -mq9.cxx MQ9Sample.java my9221-ledbar.cxx MY9221_ledbarSample.java -my9221-updown.cxx MY9221_updownSample.java -nrf24l01-broadcast.cxx NRF24L01_broadcastSample.java nrf24l01-receiver.cxx NRF24L01_receiverSample.java nrf24l01-transmitter.cxx NRF24L01_transmitterSample.java nunchuck.cxx NUNCHUCKSample.java otp538u.cxx OTP538USample.java -pca9685.cxx PCA9685Sample.java -pn532-writeurl.cxx PN532_writeurlSample.java -pn532.cxx PN532Sample.java ppd42ns.cxx PPD42NSSample.java pulsensor.cxx PulsensorSample.java rfr359f.cxx RFR359FSample.java -rgbringcoder.cxx RGBRingCoderSample.java rotaryencoder.cxx RotaryEncoderSample.java rpr220-intr.cxx RPR220_intrSample.java rpr220.cxx RPR220Sample.java -sainsmartks.cxx SAINSMARTKSSample.java -sm130.cxx SM130Sample.java -ssd1306-oled.cxx SSD1306_oledSample.java ssd1308-oled.cxx SSD1308_oledSample.java ssd1327-oled.cxx SSD1327_oledSample.java st7735.cxx ST7735Sample.java stepmotor.cxx StepMotorSample.java -ta12200.cxx TA12200Sample.java tm1637.cxx TM1637Sample.java tp401.cxx TP401Sample.java tsl2561.cxx TSL2561Sample.java ttp223.cxx TTP223Sample.java ublox6.cxx Ublox6Sample.java uln200xa.cxx ULN200XASample.java -waterlevel.cxx WaterLevelSample.java wt5001.cxx WT5001Sample.java yg1006.cxx YG1006Sample.java diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index c2c8fd99..d3b03e89 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -61,9 +61,9 @@ add_example(HMTRPSample hmtrp) add_example(HP20xExample hp20x) add_example(HTU21DSample htu21d) add_example(Itg3200Sample itg3200) -add_example(Joystick12_exampleSample joystick12) +add_example(Joystick12Sample joystick12) add_example(LDT0028Sample ldt0028) -add_example(LoL_exampleSample lol) +add_example(LoLSample lol) add_example(LSM303Sample lsm303) add_example(M24LR64ESample m24lr64e) add_example(MAX44000Sample max44000) diff --git a/examples/java/Joystick12Sample.java b/examples/java/Joystick12Sample.java new file mode 100644 index 00000000..31660bc3 --- /dev/null +++ b/examples/java/Joystick12Sample.java @@ -0,0 +1,44 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2015 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class Joystick12Sample { + + public static void main(String[] args) throws InterruptedException { + //! [Interesting] + // Instantiate a joystick on analog pins A0 and A1 + upm_joystick12.Joystick12 joystick = new upm_joystick12.Joystick12(0,1); + + // Print the X and Y input values every second + while(true){ + float x = joystick.getXInput(); + float y = joystick.getYInput(); + + System.out.println("Driving X: " + x + " , and Y: " + y ); + + Thread.sleep(1000); + } + //! [Interesting] + } + +} diff --git a/examples/java/LoLSample.java b/examples/java/LoLSample.java new file mode 100644 index 00000000..c37a6cae --- /dev/null +++ b/examples/java/LoLSample.java @@ -0,0 +1,47 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2015 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +//NOT TESTED!!! +public class LoLSample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + upm_lol.LoL sensor = new upm_lol.LoL(); + + int x = 0, y = 0; + while (true) { + // revert pixel + sensor.setPixel(x, y, sensor.getPixel(x, y)); + + if (++x == 13) { + x = 0; + y++; + } + if (y == 9) + y = 0; + Thread.sleep(10); + } + // ! [Interesting] + } +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1297c753..3de28a02 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,14 @@ +macro (file_to_list readfile outlist) + FILE(READ "${readfile}" contents) + STRING(REGEX REPLACE ";" "\\\\;" contents "${contents}") + STRING(REGEX REPLACE "\n" ";" contents "${contents}") + set("${outlist}" "${contents}" ) +endmacro() + +file_to_list ("javaswig_blacklist" JAVASWIG_BLACKLIST) +file_to_list ("pythonswig_blacklist" PYTHONSWIG_BLACKLIST) +file_to_list ("nodeswig_blacklist" NODESWIG_BLACKLIST) + macro(subdirlist result curdir) file(GLOB children RELATIVE ${curdir} ${curdir}/*) set(dirlist "") @@ -159,9 +170,6 @@ if (SWIG_FOUND) endmacro() endif(BUILDSWIGNODE) -# if(BUILDSWIGJAVA) -# add_subdirectory (java) -# endif(BUILDSWIGJAVA) endif() @@ -181,9 +189,15 @@ macro(upm_module_init) ) upm_create_install_pkgconfig (upm-${libname}.pc ${CMAKE_INSTALL_LIBDIR}/pkgconfig) if (SWIG_FOUND) - upm_swig_python() - upm_swig_node() - upm_swig_java() + if (NOT ";${PYTHONSWIG_BLACKLIST};" MATCHES ";${libname};") + upm_swig_python() + endif() + if (NOT ";${NODESWIG_BLACKLIST};" MATCHES ";${libname};") + upm_swig_node() + endif() + if (NOT ";${JAVASWIG_BLACKLIST};" MATCHES ";${libname};") + upm_swig_java() + endif() endif() if (BUILDDOC) upm_doxygen() diff --git a/src/javaswig_blacklist b/src/javaswig_blacklist new file mode 100644 index 00000000..f38ff790 --- /dev/null +++ b/src/javaswig_blacklist @@ -0,0 +1 @@ +nrf8001 diff --git a/src/nodeswig_blacklist b/src/nodeswig_blacklist new file mode 100644 index 00000000..e69de29b diff --git a/src/pythonswig_blacklist b/src/pythonswig_blacklist new file mode 100644 index 00000000..e69de29b diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..62c39ebb --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,17 @@ +add_test (NAME check_consistency COMMAND ${PYTHON_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/check_consistency.py + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + +if (BUILDSWIGJAVA) + add_test (NAME check_autoloadlibrary COMMAND ${PYTHON_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/check_autoloadlibrary.py + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + + add_test (NAME check_samplenames COMMAND ${PYTHON_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/check_samplenames.py + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + + add_test (NAME check_clean COMMAND ${PYTHON_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/check_clean.py + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +endif () diff --git a/tests/check_autoloadlibrary.py b/tests/check_autoloadlibrary.py new file mode 100755 index 00000000..29bf490f --- /dev/null +++ b/tests/check_autoloadlibrary.py @@ -0,0 +1,44 @@ +#!/usr/bin/python + +import unittest as u +import re, fnmatch, os + +rootDir = '../src/' +prefix = """ +%pragma(java) jniclasscode=%{ + static { + try { + System.loadLibrary(\"""" + +sufix = """\"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. \\n" + e); + System.exit(1); + } + } +%}""" + +class AutoLoadLibrary(u.TestCase): + + def test_existing_snippet(self): + broken_modules = [] + + for subdir, dirs, files in os.walk(rootDir): + for fileName in files: + if fnmatch.fnmatch(fileName, 'javaupm_*.i'): + moduleName = fileName[:-2] + snippet = prefix + moduleName + sufix + + with open(os.path.join(subdir, fileName), "r") as f: + if not snippet in f.read(): + broken_modules.append(moduleName) + + self.assertEqual( len(broken_modules), 0, + "\nThe following modules do not contain the standard auto load library code:\n" + \ + "\n".join(broken_modules) + \ + "\nConsider adding the following snippet to the SWIG interface file:\n" + \ + prefix + "" + sufix) + + +if __name__ == '__main__': + u.main() diff --git a/tests/check_clean.py b/tests/check_clean.py new file mode 100755 index 00000000..2031803f --- /dev/null +++ b/tests/check_clean.py @@ -0,0 +1,35 @@ +#!/usr/bin/python + +import unittest as u +import re, fnmatch, os + +rootDir = '../build/src/' +swigtypeStr = 'SWIGTYPE' + +class Clean(u.TestCase): + + def test_existing_swigtype(self): + unclean = [] + + for dirName in os.listdir(rootDir): + dirPath = os.path.join(rootDir, dirName) + if not os.path.isdir(dirPath): + continue + + ok = True + for subdir, dirs, files in os.walk(dirPath): + if not ok: + break + for fileName in files: + if swigtypeStr in fileName: + unclean.append(dirName) + ok = False + break + + self.assertEqual( len(unclean), 0, + "\nThe following modules have unclean Java bindings:\n" + \ + "\n".join(unclean) + "\n\n" + \ + "Consider adding them to the SWIGJAVA blacklist") + +if __name__ == '__main__': + u.main() diff --git a/tests/check_consistency.py b/tests/check_consistency.py new file mode 100755 index 00000000..c1308f8a --- /dev/null +++ b/tests/check_consistency.py @@ -0,0 +1,60 @@ +#!/usr/bin/python + +import unittest as u +import re, fnmatch, os + +rootDir = '../src/' +javaBlacklistFile = '../src/javaswig_blacklist' +pythonBlacklistFile = '../src/pythonswig_blacklist' +nodeBlacklistFile = '../src/nodeswig_blacklist' + + +class BlacklistConsistency(u.TestCase): + + def test_java_blacklist(self): + + with open(javaBlacklistFile) as f: + blacklist = [line.rstrip('\n') for line in f] + + for libraryName in blacklist: + files = os.listdir( os.path.join(rootDir, libraryName)) + interfaceFileName = "javaupm_" + libraryName + ".i" + + if interfaceFileName in files: + self.fail("\n" + libraryName + " is in javaswig blacklist.\n" + \ + "Remove it from blacklist or remove " + \ + interfaceFileName + " from sources.") + + + def test_python_blacklist(self): + + with open(pythonBlacklistFile) as f: + blacklist = [line.rstrip('\n') for line in f] + + for libraryName in blacklist: + files = os.listdir( os.path.join(rootDir, libraryName)) + interfaceFileName = "pyupm_" + libraryName + ".i" + + if interfaceFileName in files: + self.fail("\n" + libraryName + " is in pythonswig blacklist.\n" + \ + "Remove it from blacklist or remove " + \ + interfaceFileName + " from sources.") + + + def test_node_blacklist(self): + + with open(nodeBlacklistFile) as f: + blacklist = [line.rstrip('\n') for line in f] + + for libraryName in blacklist: + files = os.listdir( os.path.join(rootDir, libraryName)) + interfaceFileName = "jsupm_" + libraryName + ".i" + + if interfaceFileName in files: + self.fail("\n" + libraryName + " is in nodeswig blacklist.\n" + \ + "Remove it from blacklist or remove " + \ + interfaceFileName + " from sources.") + + +if __name__ == '__main__': + u.main() diff --git a/tests/check_samplenames.py b/tests/check_samplenames.py new file mode 100755 index 00000000..c1edcc2d --- /dev/null +++ b/tests/check_samplenames.py @@ -0,0 +1,53 @@ +#!/usr/bin/python + +import unittest as u +import re, fnmatch, os, sys + +sampleMappingFile = '../doxy/samples.mapping.txt' +cSamplesDir = '../examples/c++/' +javaSamplesDir = '../examples/java/' + + +class SampleNames(u.TestCase): + + def test_existing_samples(self): + missing_c_files = [] + missing_java_files = [] + + with open (sampleMappingFile, "r") as f: + for line in f: + sampleNames = line.split(); + + cSampleName = sampleNames[0] + if not cSampleName.endswith('.cxx'): + continue + + javaSampleName = sampleNames[1] + if not javaSampleName.endswith('.java'): + continue + + ok = False + for file in os.listdir(cSamplesDir): + if file == cSampleName: + ok = True + break + + if not ok: + missing_c_files.append(cSampleName) + + ok = False + for file in os.listdir(javaSamplesDir): + if file == javaSampleName: + ok = True + break + + if not ok: + missing_java_files.append(javaSampleName) + + self.assertEqual( len(missing_java_files) + len(missing_c_files), 0, + "\nThe following files are missing from samples:\n" + \ + "\n".join(missing_c_files) + "\n" + "\n".join(missing_java_files)) + +if __name__ == '__main__': + u.main() +