1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-18 13:55:18 +03:00

Move zigzag encoding function to new common/encoding.c file, add tests

This commit is contained in:
Nicholas Sherlock 2015-02-26 13:27:09 +13:00
parent aae9141120
commit 451d3d7ba2
8 changed files with 164 additions and 21 deletions

View file

@ -0,0 +1,84 @@
/*
* This file is part of Cleanflight.
*
* Cleanflight 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 3 of the License, or
* (at your option) any later version.
*
* Cleanflight 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 Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
extern "C" {
#include "common/encoding.h"
}
#include "unittest_macros.h"
#include "gtest/gtest.h"
typedef struct zigzagEncodingExpectation_t {
int32_t input;
uint32_t expected;
} zigzagEncodingExpectation_t;
typedef struct floatToIntEncodingExpectation_t {
float input;
uint32_t expected;
} floatToIntEncodingExpectation_t;
TEST(EncodingTest, ZigzagEncodingTest)
{
// given
zigzagEncodingExpectation_t expectations[] = {
{ 0, 0},
{-1, 1},
{ 1, 2},
{-2, 3},
{ 2, 4},
{ 2147483646, 4294967292},
{-2147483647, 4294967293},
{ 2147483647, 4294967294},
{-2147483648, 4294967295},
};
int expectationCount = sizeof(expectations) / sizeof(expectations[0]);
// expect
for (int i = 0; i < expectationCount; i++) {
zigzagEncodingExpectation_t *expectation = &expectations[i];
EXPECT_EQ(expectation->expected, zigzagEncode(expectation->input));
}
}
TEST(EncodingTest, FloatToIntEncodingTest)
{
// given
floatToIntEncodingExpectation_t expectations[] = {
{0.0, 0x00000000},
{2.0, 0x40000000}, // Exponent should be in the top bits
{4.5, 0x40900000}
};
int expectationCount = sizeof(expectations) / sizeof(expectations[0]);
// expect
for (int i = 0; i < expectationCount; i++) {
floatToIntEncodingExpectation_t *expectation = &expectations[i];
EXPECT_EQ(expectation->expected, castFloatBytesToInt(expectation->input));
}
}
// STUBS
extern "C" {
}