diff --git a/backend/genesys_low.h b/backend/genesys_low.h index 277710489..e75080839 100644 --- a/backend/genesys_low.h +++ b/backend/genesys_low.h @@ -461,6 +461,11 @@ struct GenesysRegisterSetting { uint16_t address = 0; uint8_t value = 0; uint8_t mask = 0xff; + + bool operator==(const GenesysRegisterSetting& other) const + { + return address == other.address && value == other.value && mask == other.mask; + } }; template @@ -524,6 +529,11 @@ public: friend void serialize(std::istream& str, GenesysRegisterSettingSet& reg); friend void serialize(std::ostream& str, GenesysRegisterSettingSet& reg); + bool operator==(const GenesysRegisterSettingSet& other) const + { + return regs_ == other.regs_; + } + private: std::vector regs_; }; @@ -545,6 +555,11 @@ struct GenesysFrontendLayout { std::array offset_addr = {}; std::array gain_addr = {}; + + bool operator==(const GenesysFrontendLayout& other) const + { + return offset_addr == other.offset_addr && gain_addr == other.gain_addr; + } }; /** @brief Data structure to set up analog frontend. @@ -586,6 +601,14 @@ struct Genesys_Frontend { return regs.get_value(layout.gain_addr[which]); } + + bool operator==(const Genesys_Frontend& other) const + { + return fe_id == other.fe_id && + regs == other.regs && + reg2 == other.reg2 && + layout == other.layout; + } }; template @@ -658,6 +681,29 @@ struct Genesys_Sensor { } return 1; } + + bool operator==(const Genesys_Sensor& other) const + { + return sensor_id == other.sensor_id && + optical_res == other.optical_res && + min_resolution == other.min_resolution && + max_resolution == other.max_resolution && + method == other.method && + ccd_size_divisor == other.ccd_size_divisor && + black_pixels == other.black_pixels && + dummy_pixel == other.dummy_pixel && + CCD_start_xoffset == other.CCD_start_xoffset && + sensor_pixels == other.sensor_pixels && + fau_gain_white_ref == other.fau_gain_white_ref && + gain_white_ref == other.gain_white_ref && + exposure.blue == other.exposure.blue && + exposure.green == other.exposure.green && + exposure.red == other.exposure.red && + exposure_lperiod == other.exposure_lperiod && + custom_regs == other.custom_regs && + custom_fe_regs == other.custom_fe_regs && + gamma == other.gamma; + } }; template @@ -1245,6 +1291,22 @@ struct SetupParams { throw std::runtime_error("SetupParams are not valid"); } } + + bool operator==(const SetupParams& other) const + { + return xres == other.xres && + yres == other.yres && + startx == other.startx && + starty == other.starty && + pixels == other.pixels && + lines == other.lines && + depth == other.depth && + channels == other.channels && + scan_method == other.scan_method && + scan_mode == other.scan_mode && + color_filter == other.color_filter && + flags == other.flags; + } }; template @@ -1289,6 +1351,21 @@ struct Genesys_Current_Setup SANE_Int stagger = 0; // max shift of any ccd component, including staggered pixels SANE_Int max_shift = 0; + + bool operator==(const Genesys_Current_Setup& other) const + { + return params == other.params && + pixels == other.pixels && + lines == other.lines && + depth == other.depth && + channels == other.channels && + exposure_time == other.exposure_time && + xres == other.xres && + yres == other.yres && + ccd_size_divisor == other.ccd_size_divisor && + stagger == other.stagger && + max_shift == other.max_shift; + } }; template @@ -1355,6 +1432,19 @@ struct Genesys_Calibration_Cache size_t average_size = 0; std::vector white_average_data; std::vector dark_average_data; + + bool operator==(const Genesys_Calibration_Cache& other) const + { + return used_setup == other.used_setup && + last_calibration == other.last_calibration && + frontend == other.frontend && + sensor == other.sensor && + calib_pixels == other.calib_pixels && + calib_channels == other.calib_channels && + average_size == other.average_size && + white_average_data == other.white_average_data && + dark_average_data == other.dark_average_data; + } }; template diff --git a/configure.ac b/configure.ac index 65176f9c7..376f29049 100644 --- a/configure.ac +++ b/configure.ac @@ -785,7 +785,10 @@ AC_SUBST(AM_LDFLAGS) AC_CONFIG_FILES([Makefile lib/Makefile sanei/Makefile frontend/Makefile \ japi/Makefile backend/Makefile include/Makefile doc/Makefile \ - po/Makefile.in testsuite/Makefile testsuite/sanei/Makefile testsuite/tools/Makefile \ + po/Makefile.in testsuite/Makefile \ + testsuite/backend/Makefile \ + testsuite/backend/genesys/Makefile \ + testsuite/sanei/Makefile testsuite/tools/Makefile \ tools/Makefile doc/doxygen-sanei.conf doc/doxygen-genesys.conf]) AC_CONFIG_FILES([tools/sane-config], [chmod a+x tools/sane-config]) AC_CONFIG_FILES([tools/sane-backends.pc]) diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am index 23207b654..01282d4bf 100644 --- a/testsuite/Makefile.am +++ b/testsuite/Makefile.am @@ -4,7 +4,7 @@ ## This file is part of the "Sane" build infra-structure. See ## included LICENSE file for license information. -SUBDIRS = sanei tools +SUBDIRS = backend sanei tools SCANIMAGE = ../frontend/scanimage$(EXEEXT) TESTFILE = $(srcdir)/testfile.pnm diff --git a/testsuite/backend/Makefile.am b/testsuite/backend/Makefile.am new file mode 100644 index 000000000..1f7d30e85 --- /dev/null +++ b/testsuite/backend/Makefile.am @@ -0,0 +1,7 @@ +## Makefile.am -- an automake template for Makefile.in file +## Copyright (C) 2019 Sane Developers. +## +## This file is part of the "Sane" build infra-structure. See +## included LICENSE file for license information. + +SUBDIRS = genesys diff --git a/testsuite/backend/genesys/Makefile.am b/testsuite/backend/genesys/Makefile.am new file mode 100644 index 000000000..1332cf819 --- /dev/null +++ b/testsuite/backend/genesys/Makefile.am @@ -0,0 +1,23 @@ +## Makefile.am -- an automake template for Makefile.in file +## Copyright (C) 2019 Sane Developers. +## +## This file is part of the "Sane" build infra-structure. See +## included LICENSE file for license information. + +TEST_LDADD = \ + ../../../sanei/libsanei.la \ + ../../../lib/liblib.la \ + ../../../backend/libgenesys.la \ + ../../../backend/sane_strstatus.lo \ + $(MATH_LIB) $(USB_LIBS) $(PTHREAD_LIBS) + +check_PROGRAMS = genesys_tests +TESTS = $(check_PROGRAMS) + +AM_CPPFLAGS += -I. -I$(srcdir) -I$(top_builddir)/include -I$(top_srcdir)/include $(USB_CFLAGS) \ + -DBACKEND_NAME=genesys + +genesys_tests_SOURCES = tests.cc tests.h minigtest.cc minigtest.h \ + tests_calibration.cc + +genesys_tests_LDADD = $(TEST_LDADD) diff --git a/testsuite/backend/genesys/minigtest.cc b/testsuite/backend/genesys/minigtest.cc new file mode 100644 index 000000000..5ca73c2ff --- /dev/null +++ b/testsuite/backend/genesys/minigtest.cc @@ -0,0 +1,35 @@ +/* sane - Scanner Access Now Easy. + + Copyright (C) 2019 Povilas Kanapickas + + This file is part of the SANE package. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA. +*/ + +#include "minigtest.h" + +size_t s_num_successes = 0; +size_t s_num_failures = 0; + +int finish_tests() +{ + std::cerr << "Finished tests. Sucessses: " << s_num_successes + << " failures: " << s_num_failures << "\n"; + if (s_num_failures > 0) + return 1; + return 0; +} diff --git a/testsuite/backend/genesys/minigtest.h b/testsuite/backend/genesys/minigtest.h new file mode 100644 index 000000000..752efe160 --- /dev/null +++ b/testsuite/backend/genesys/minigtest.h @@ -0,0 +1,77 @@ +/* sane - Scanner Access Now Easy. + + Copyright (C) 2019 Povilas Kanapickas + + This file is part of the SANE package. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA. +*/ + +#ifndef SANE_TESTSUITE_BACKEND_GENESYS_MINIGTEST_H +#define SANE_TESTSUITE_BACKEND_GENESYS_MINIGTEST_H + +#include +#include + +extern size_t s_num_successes; +extern size_t s_num_failures; + +inline void print_location(std::ostream& out, const char* function, const char* path, + unsigned line) +{ + out << path << ":" << line << " in " << function; +} + +template +void check_equal(const T& t, const U& u, const char* function, const char* path, unsigned line) +{ + if (t != u) { + s_num_failures++; + std::cerr << "FAILURE at "; + print_location(std::cerr, function, path, line); + std::cerr << " :\n" << t << " != " << u << "\n\n"; + } else { + s_num_successes++; + std::cerr << "SUCCESS at "; + print_location(std::cerr, function, path, line); + std::cerr << "\n"; + } +} + +inline void check_true(bool x, const char* function, const char* path, unsigned line) +{ + if (x) { + s_num_successes++; + std::cerr << "SUCCESS at "; + } else { + s_num_failures++; + std::cerr << "FAILURE at "; + } + print_location(std::cerr, function, path, line); + std::cerr << "\n"; +} + + +#define ASSERT_EQ(x, y) do { check_equal((x), (y), __func__, __FILE__, __LINE__); } \ + while (false) +#define ASSERT_TRUE(x) do { check_true(bool(x), __func__, __FILE__, __LINE__); } \ + while (false) +#define ASSERT_FALSE(x) do { !check_true(bool(x), __func__, __FILE__, __LINE__); } \ + while (false) + +int finish_tests(); + +#endif diff --git a/testsuite/backend/genesys/tests.cc b/testsuite/backend/genesys/tests.cc new file mode 100644 index 000000000..40b1b3e25 --- /dev/null +++ b/testsuite/backend/genesys/tests.cc @@ -0,0 +1,30 @@ +/* sane - Scanner Access Now Easy. + + Copyright (C) 2019 Povilas Kanapickas + + This file is part of the SANE package. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA. +*/ + +#include "tests.h" +#include "minigtest.h" + +int main() +{ + test_calibration_parsing(); + return finish_tests(); +} diff --git a/testsuite/backend/genesys/tests.h b/testsuite/backend/genesys/tests.h new file mode 100644 index 000000000..f4e4d2e4e --- /dev/null +++ b/testsuite/backend/genesys/tests.h @@ -0,0 +1,28 @@ +/* sane - Scanner Access Now Easy. + + Copyright (C) 2019 Povilas Kanapickas + + This file is part of the SANE package. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA. +*/ + +#ifndef SANE_TESTSUITE_BACKEND_GENESYS_GENESYS_UNIT_TEST_H +#define SANE_TESTSUITE_BACKEND_GENESYS_GENESYS_UNIT_TEST_H + +void test_calibration_parsing(); + +#endif diff --git a/testsuite/backend/genesys/tests_calibration.cc b/testsuite/backend/genesys/tests_calibration.cc new file mode 100644 index 000000000..959037a41 --- /dev/null +++ b/testsuite/backend/genesys/tests_calibration.cc @@ -0,0 +1,130 @@ +/* sane - Scanner Access Now Easy. + + Copyright (C) 2019 Povilas Kanapickas + + This file is part of the SANE package. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA. +*/ + +#include "tests.h" +#include "minigtest.h" + +#include "../../../backend/genesys_low.h" + +#include + +#define DEBUG_DECLARE_ONLY + +Genesys_Calibration_Cache create_fake_calibration_entry() +{ + Genesys_Calibration_Cache calib; + calib.used_setup.pixels = 10020; + calib.used_setup.lines = 150; + calib.used_setup.xres = 100.5; + + GenesysFrontendLayout wolfson_layout; + wolfson_layout.offset_addr = { 0x20, 0x21, 0x22 }; + wolfson_layout.gain_addr = { 0x28, 0x29, 0x2a }; + + Genesys_Frontend fe; + fe.fe_id = DAC_WOLFSON_UMAX; + fe.layout = wolfson_layout; + fe.regs = { + { 0x00, 0x00 }, + { 0x01, 0x03 }, + { 0x02, 0x05 }, + { 0x03, 0x11 }, + { ' ', 0x80 }, // check whether space-like integer values are correctly serialized + { ',', 0x80 }, + { '\r', '\n' }, + { '\n', 0x00 }, + { 0x25, 0x00 }, + { 0x26, 0x00 }, + { 0x28, 0x02 }, + { 0x29, 0x02 }, + { 0x2a, 0x02 }, + }; + fe.reg2 = {0x00, 0x00, 0x00}; + calib.frontend = fe; + + Genesys_Sensor sensor; + sensor.sensor_id = CCD_UMAX; + sensor.optical_res = 1200; + sensor.black_pixels = 48; + sensor.dummy_pixel = 64; + sensor.CCD_start_xoffset = 0; + sensor.sensor_pixels = 10800; + sensor.fau_gain_white_ref = 210; + sensor.gain_white_ref = 230; + sensor.exposure = { 0x0000, 0x0000, 0x0000 }; + sensor.custom_regs = { + { 0x08, 0x01 }, + { 0x09, 0x03 }, + { 0x0a, 0x05 }, + { 0x0b, 0x07 }, + { 0x16, 0x33 }, + { 0x17, 0x05 }, + { 0x18, 0x31 }, + { 0x19, 0x2a }, + { 0x1a, 0x00 }, + { 0x1b, 0x00 }, + { 0x1c, 0x00 }, + { 0x1d, 0x02 }, + { 0x52, 0x13 }, + { 0x53, 0x17 }, + { 0x54, 0x03 }, + { 0x55, 0x07 }, + { 0x56, 0x0b }, + { 0x57, 0x0f }, + { 0x58, 0x23 }, + { 0x59, 0x00 }, + { 0x5a, 0xc1 }, + { 0x5b, 0x00 }, + { 0x5c, 0x00 }, + { 0x5d, 0x00 }, + { 0x5e, 0x00 }, + }; + sensor.gamma = {1.0, 1.0, 1.0}; + calib.sensor = sensor; + + calib.calib_pixels = 12345; + calib.calib_channels = 3; + calib.average_size = 7; + calib.white_average_data = { 8, 7, 6, 5, 4, 3, 2 }; + calib.dark_average_data = { 6, 5, 4, 3, 2, 18, 12 }; + return calib; +} + +void test_calibration_roundtrip() +{ + Genesys_Device::Calibration calibration = { create_fake_calibration_entry() }; + Genesys_Device::Calibration deserialized; + + std::stringstream str; + serialize(static_cast(str), calibration); + serialize(static_cast(str), deserialized); + ASSERT_TRUE(calibration == deserialized); + + int x; + str >> x; + ASSERT_TRUE(str.eof()); +} + +void test_calibration_parsing() +{ + test_calibration_roundtrip(); +}