| 1 | // Written by James Turner, started 2017. |
| 2 | // |
| 3 | // Copyright (C) 2017 James Turner |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or |
| 6 | // modify it under the terms of the GNU General Public License as |
| 7 | // published by the Free Software Foundation; either version 2 of the |
| 8 | // License, or (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, but |
| 11 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | // General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program; if not, write to the Free Software |
| 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | |
| 19 | #include "config.h" |
| 20 | |
| 21 | #include "test_hidinput.hxx" |
| 22 | |
| 23 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 24 | |
| 25 | #include <simgear/misc/test_macros.hxx> |
| 26 | |
| 27 | #include <Input/FGHIDEventInput.hxx> |
| 28 | |
| 29 | void HIDInputTests::testValueExtract() |
| 30 | { |
| 31 | uint8_t testDataFromSpec[4] = {0, 0xf4, 0x1 | (0x7 << 2), 0x03}; |
| 32 | CPPUNIT_ASSERT(extractBits(testDataFromSpec, 4, 8, 10) == 500); |
| 33 | CPPUNIT_ASSERT(extractBits(testDataFromSpec, 4, 18, 10) == 199); |
| 34 | |
| 35 | uint8_t testData2[4] = {0x01 << 6 | 0x0f, |
| 36 | 0x17 | (1 << 6), |
| 37 | 0x3 | (0x11 << 2), |
| 38 | 0x3d | (1 << 6) }; |
| 39 | |
| 40 | CPPUNIT_ASSERT(extractBits(testData2, 4, 0, 6) == 15); |
| 41 | CPPUNIT_ASSERT(extractBits(testData2, 4, 6, 12) == 3421); |
| 42 | CPPUNIT_ASSERT(extractBits(testData2, 4, 18, 12) == 3921); |
| 43 | CPPUNIT_ASSERT(extractBits(testData2, 4, 30, 1) == 1); |
| 44 | CPPUNIT_ASSERT(extractBits(testData2, 4, 31, 1) == 0); |
| 45 | } |
| 46 | |
| 47 | // void writeBits(uint8_t* bytes, size_t bitOffset, size_t bitSize, int value) |
| 48 | |
| 49 | void HIDInputTests::testValueInsert() |
| 50 | { |
| 51 | uint8_t buf[8]; |
| 52 | memset(buf, 0, 8); |
| 53 | |
| 54 | int a = 3421; |
| 55 | int b = 3921; |
| 56 | writeBits(buf, 6, 12, a); |
| 57 | writeBits(buf, 18, 12, b); |
| 58 | |
| 59 | CPPUNIT_ASSERT(buf[0] == 0x40); |
| 60 | CPPUNIT_ASSERT(buf[1] == 0x57); |
| 61 | CPPUNIT_ASSERT(buf[2] == (0x03 | 0x44)); |
| 62 | CPPUNIT_ASSERT(buf[3] == 0x3d); |
| 63 | } |
| 64 | |
| 65 | void HIDInputTests::testSignExtension() |
| 66 | { |
| 67 | CPPUNIT_ASSERT(signExtend(0x80, 8) == -128); |
| 68 | CPPUNIT_ASSERT(signExtend(0xff, 8) == -1); |
| 69 | CPPUNIT_ASSERT(signExtend(0x7f, 8) == 127); |
| 70 | |
| 71 | CPPUNIT_ASSERT(signExtend(0x831, 12) == -1999); |
| 72 | CPPUNIT_ASSERT(signExtend(0x7dd, 12) == 2013); |
| 73 | } |