Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Jun 1, 2022
1 parent 4c0b64d commit c0337b9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/unittests/filter_iterator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,34 @@ std::string remove_space(std::string_view in)
skip_space_iterator{in_buffer.end(), in_buffer.end()}, std::back_inserter(out));
return out;
}

bool is_positive(int x) noexcept
{
return x > 0;
}
} // namespace


TEST(filter_iterator, filter_positive_integers)
{
std::vector<int> in{1, 0, 0, 2, -3, 3, 4, 5, 0, 6, 7, -1, -2, 0, 8, 9, -10};
std::vector<int> out;

using iter = evmc::filter_iterator<std::vector<int>::const_iterator, is_positive>;
std::copy(iter{in.begin(), in.end()}, iter{in.end(), in.end()}, std::back_inserter(out));
ASSERT_EQ(out.size(), 9u);
EXPECT_EQ(out[0], 1);
EXPECT_EQ(out[1], 2);
EXPECT_EQ(out[2], 3);
EXPECT_EQ(out[3], 4);
EXPECT_EQ(out[4], 5);
EXPECT_EQ(out[5], 6);
EXPECT_EQ(out[6], 7);
EXPECT_EQ(out[7], 8);
EXPECT_EQ(out[8], 9);
}


TEST(skip_space_iterator, empty)
{
EXPECT_EQ(remove_space(""), "");
Expand Down
21 changes: 21 additions & 0 deletions test/unittests/hex_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
// Licensed under the Apache License, Version 2.0.

#include <evmc/hex.hpp>
#include <tools/evmc/filter_iterator.hpp>
#include <gtest/gtest.h>

using namespace evmc;

namespace
{} // namespace

TEST(hex, hex_of_byte)
{
EXPECT_EQ(hex(0x00), "00");
Expand Down Expand Up @@ -72,3 +76,20 @@ TEST(hex, validate_hex)
EXPECT_FALSE(validate_hex("0"));
EXPECT_FALSE(validate_hex("WXYZ"));
}

TEST(hex, from_hex_skip_space)
{
// Combine from_hex with skip_space_iterator.
static constexpr auto from_hex_skip_space = [](std::string_view hex) {
bytes out;
const auto status =
from_hex(skip_space_iterator{hex.begin(), hex.end()},
skip_space_iterator{hex.end(), hex.end()}, std::back_inserter(out));
EXPECT_TRUE(status);
return out;
};
EXPECT_EQ(from_hex_skip_space("0x010203"), (bytes{0x01, 0x02, 0x03}));
EXPECT_EQ(from_hex_skip_space("0x 010203 "), (bytes{0x01, 0x02, 0x03}));
EXPECT_EQ(from_hex_skip_space(" 0 x 0 1 0 2 0 3 "), (bytes{0x01, 0x02, 0x03}));
EXPECT_EQ(from_hex_skip_space("\f 0\r x 0 1\t 0 2 \v0 3 \n"), (bytes{0x01, 0x02, 0x03}));
}

0 comments on commit c0337b9

Please sign in to comment.