|
| 1 | +// EVMC: Ethereum Client-VM Connector API. |
| 2 | +// Copyright 2022 The EVMC Authors. |
| 3 | +// Licensed under the Apache License, Version 2.0. |
| 4 | + |
| 5 | +#include <tools/evmc/skip_space_iterator.hpp> |
| 6 | +#include <gtest/gtest.h> |
| 7 | + |
| 8 | +using evmc::skip_space_iterator; |
| 9 | + |
| 10 | +namespace |
| 11 | +{ |
| 12 | +std::string remove_space(std::string_view in) |
| 13 | +{ |
| 14 | + // Copy input to additional buffer. This helps with out-of-buffer reads detection by sanitizers. |
| 15 | + const auto in_buffer = std::make_unique<char[]>(in.size()); |
| 16 | + const auto begin = in_buffer.get(); |
| 17 | + const auto end = begin + in.size(); |
| 18 | + std::copy(in.begin(), in.end(), begin); |
| 19 | + |
| 20 | + // Filter the input. |
| 21 | + std::string out; |
| 22 | + std::copy(skip_space_iterator{begin, end}, skip_space_iterator{end, end}, |
| 23 | + std::back_inserter(out)); |
| 24 | + return out; |
| 25 | +} |
| 26 | +} // namespace |
| 27 | + |
| 28 | +TEST(skip_space_iterator, empty) |
| 29 | +{ |
| 30 | + EXPECT_EQ(remove_space(""), ""); |
| 31 | + EXPECT_EQ(remove_space(" "), ""); |
| 32 | + EXPECT_EQ(remove_space(" "), ""); |
| 33 | +} |
| 34 | + |
| 35 | +TEST(skip_space_iterator, filter_middle) |
| 36 | +{ |
| 37 | + EXPECT_EQ(remove_space("x y"), "xy"); |
| 38 | + EXPECT_EQ(remove_space("x y"), "xy"); |
| 39 | +} |
| 40 | + |
| 41 | +TEST(skip_space_iterator, filter_front) |
| 42 | +{ |
| 43 | + EXPECT_EQ(remove_space(" x"), "x"); |
| 44 | + EXPECT_EQ(remove_space(" x"), "x"); |
| 45 | +} |
| 46 | + |
| 47 | +TEST(skip_space_iterator, filter_back) |
| 48 | +{ |
| 49 | + EXPECT_EQ(remove_space("x "), "x"); |
| 50 | + EXPECT_EQ(remove_space("x "), "x"); |
| 51 | +} |
| 52 | + |
| 53 | +TEST(skip_space_iterator, filter_mixed) |
| 54 | +{ |
| 55 | + EXPECT_EQ(remove_space(" x y z "), "xyz"); |
| 56 | + EXPECT_EQ(remove_space(" x y z "), "xyz"); |
| 57 | +} |
| 58 | + |
| 59 | +TEST(skip_space_iterator, isspace) |
| 60 | +{ |
| 61 | + // Test internal isspace() compliance with std::isspace(). |
| 62 | + // The https://en.cppreference.com/w/cpp/string/byte/isspace has the list of "space" characters. |
| 63 | + |
| 64 | + for (int i = int{std::numeric_limits<char>::min()}; i <= std::numeric_limits<char>::max(); ++i) |
| 65 | + { |
| 66 | + const auto c = static_cast<char>(i); |
| 67 | + EXPECT_EQ(evmc::isspace(c), (std::isspace(c) != 0)); |
| 68 | + switch (c) |
| 69 | + { |
| 70 | + case ' ': |
| 71 | + case '\f': |
| 72 | + case '\n': |
| 73 | + case '\r': |
| 74 | + case '\t': |
| 75 | + case '\v': |
| 76 | + EXPECT_TRUE(evmc::isspace(c)); |
| 77 | + break; |
| 78 | + default: |
| 79 | + EXPECT_FALSE(evmc::isspace(c)); |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments