Skip to content

Commit

Permalink
⚗️ (stl): Test std::ranges support for arm-gcc + clang
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Feb 28, 2023
1 parent ec914e2 commit 8fed903
Show file tree
Hide file tree
Showing 8 changed files with 376 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ if(${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests")
tests/MathUtils_test_map.cpp
tests/MathUtils_test_random8.cpp
tests/MemoryUtils_test.cpp
tests/RangesUtils_test.cpp
)
endif()
33 changes: 33 additions & 0 deletions libs/Utils/include/RangesUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <ranges>

namespace leka::utils::ranges {

struct SequenceFromTo {
int start;
int end;
};

constexpr auto sequence(const SequenceFromTo seq)
{
auto values = std::ranges::views::iota(seq.start, seq.end + 1);
return values;
}

struct SequenceFromSize {
int from;
int size;
};

constexpr auto sequence(const SequenceFromSize seq)
{
auto values = std::ranges::views::iota(seq.from, seq.from + seq.size);
return values;
}

} // namespace leka::utils::ranges
1 change: 1 addition & 0 deletions libs/Utils/include/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
#include "CastUtils.h"
#include "MathUtils.h"
#include "MemoryUtils.h"
#include "RangesUtils.h"
138 changes: 138 additions & 0 deletions libs/Utils/tests/RangesUtils_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include "Utils.h"
#include "gtest/gtest.h"

using namespace leka::utils::ranges;

TEST(RangesTests, fromStart0ToEnd5)
{
auto expected = std::to_array({0, 1, 2, 3, 4, 5});

auto i = 0;
for (auto value: sequence({.start = 0, .end = 5})) {
EXPECT_EQ(value, expected.at(i));
++i;
}

EXPECT_EQ(i, 5 + 1) << "Loop ran " << i << " times, should run " << 5 + 1 << " times";
}

TEST(RangesTests, fromStart10ToEnd20)
{
auto expected = std::to_array({10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20});

auto i = 0;
for (auto value: sequence({.start = 10, .end = 20})) {
EXPECT_EQ(value, expected.at(i));
++i;
}

EXPECT_EQ(i, 10 + 1) << "Loop ran " << i << " times, should run " << 10 + 1 << " times";
}

TEST(RangesTests, fromStart0ToEnd0)
{
auto expected = std::to_array({0});

auto i = 0;
for (auto value: sequence({.start = 0, .end = 0})) {
EXPECT_EQ(value, expected.at(i));
++i;
}

EXPECT_EQ(i, 1) << "Loop ran " << i << " times, should run " << 0 + 1 << " times";
}

TEST(RangesTests, fromStart1ToEnd1)
{
auto expected = std::to_array({1});

auto i = 0;
for (auto value: sequence({.start = 1, .end = 1})) {
EXPECT_EQ(value, expected.at(i));
++i;
}

EXPECT_EQ(i, 1) << "Loop ran " << i << " times, should run " << 0 + 1 << " times";
}

TEST(RangesTests, from0Size5)
{
auto expected = std::to_array({0, 1, 2, 3, 4});

auto i = 0;
for (auto value: sequence({.from = 0, .size = 5})) {
EXPECT_EQ(value, expected.at(i));
++i;
}

EXPECT_EQ(i, 5) << "Loop ran " << i << " times, should run " << 5 << " times";
}

TEST(RangesTests, from10Size10)
{
auto expected = std::to_array({10, 11, 12, 13, 14, 15, 16, 17, 18, 19});

auto i = 0;
for (auto value: sequence({.from = 10, .size = 10})) {
EXPECT_EQ(value, expected.at(i));
++i;
}

EXPECT_EQ(i, 10) << "Loop ran " << i << " times, should run " << 10 << " times";
}

TEST(RangesTests, from0Size0)
{
auto expected = std::to_array({0});

auto i = 0;
for (auto value: sequence({.from = 0, .size = 0})) {
EXPECT_EQ(value, expected.at(i));
++i;
}

EXPECT_EQ(i, 0) << "Loop ran " << i << " times, should run " << 0 << " times";
}

TEST(RangesTests, from0Size1)
{
auto expected = std::to_array({0});

auto i = 0;
for (auto value: sequence({.from = 0, .size = 1})) {
EXPECT_EQ(value, expected.at(i));
++i;
}

EXPECT_EQ(i, 1) << "Loop ran " << i << " times, should run " << 1 << " times";
}

TEST(RangesTests, from1Size0)
{
auto expected = std::to_array({1});

auto i = 0;
for (auto value: sequence({.from = 1, .size = 0})) {
EXPECT_EQ(value, expected.at(i));
++i;
}

EXPECT_EQ(i, 0) << "Loop ran " << i << " times, should run " << 0 << " times";
}

TEST(RangesTests, from1Size1)
{
auto expected = std::to_array({1});

auto i = 0;
for (auto value: sequence({.from = 1, .size = 1})) {
EXPECT_EQ(value, expected.at(i));
++i;
}

EXPECT_EQ(i, 1) << "Loop ran " << i << " times, should run " << 1 << " times";
}
27 changes: 27 additions & 0 deletions spikes/stl_cxxsupport/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
// SPDX-License-Identifier: Apache-2.0

#include <array>
#include <ranges>
#include <span>

#include "drivers/BufferedSerial.h"
#include "rtos/ThisThread.h"

#include "HelloWorld.h"
#include "LogKit.h"
#include "RangesUtils.h"

using namespace leka;
using namespace std::chrono;
Expand Down Expand Up @@ -94,5 +96,30 @@ auto main() -> int
auto back = span0.back();
log_info("span0.back() = %i", back);

log_info("Test std::range features");

constexpr auto range = [](const auto a, const auto b) {
auto x = std::ranges::views::iota(a) | std::ranges::views::take(b - a + 1);
return std::ranges::views::all(x);
};

for (auto x: range(1, 10)) {
log_info("x: %i", x);
}

for (auto y: range(20, 25)) {
log_info("y: %i", y);
}

log_info("Test utils::ranges::sequences features");

for (auto val: utils::ranges::sequence({.start = 0, .end = 5})) {
log_info("x: %i", val);
}

for (auto val: utils::ranges::sequence({.start = 10, .end = 20})) {
log_info("y: %i", val);
}

return 1;
}
2 changes: 2 additions & 0 deletions tests/functional/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/firmware_kit)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/imu_kit)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/io_expander)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/qdac)

add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/stl_features)
15 changes: 15 additions & 0 deletions tests/functional/tests/stl_features/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Leka - LekaOS
# Copyright 2022 APF France handicap
# SPDX-License-Identifier: Apache-2.0

register_functional_test(
TARGET
functional_ut_std_features

INCLUDE_DIRECTORIES

SOURCES
suite_std_ranges.cpp

LINK_LIBRARIES
)
Loading

0 comments on commit 8fed903

Please sign in to comment.