forked from Harrm/scale-codec
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscale_boolean_test.cpp
More file actions
66 lines (60 loc) · 1.74 KB
/
Copy pathscale_boolean_test.cpp
File metadata and controls
66 lines (60 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#include <gtest/gtest.h>
#include <qtils/test/outcome.hpp>
#include <scale/scale.hpp>
using scale::ByteArray;
using scale::DecodeError;
using scale::EncodeError;
using scale::impl::memory::decode;
using scale::impl::memory::encode;
/**
* @given bool values: true and false
* @when encode them by fixedwidth::encodeBool function
* @then obtain expected result each time
*/
TEST(ScaleBoolTest, EncodeBoolSuccess) {
{
ASSERT_OUTCOME_SUCCESS(encoded, encode(true));
ASSERT_EQ(encoded, ByteArray{0x1});
}
{
ASSERT_OUTCOME_SUCCESS(encoded, encode(false));
ASSERT_EQ(encoded, ByteArray{0x0});
}
}
/**
* @brief helper structure for testing scale::decode
*/
struct ThreeBooleans {
bool b1 = false;
bool b2 = false;
bool b3 = false;
};
/**
* @given byte array containing values {0, 1, 2}
* @when scale::decode function is applied sequentially
* @then it returns false, true and kUnexpectedValue error correspondingly,
* and in the end no more bytes left in stream
*/
TEST(ScaleBoolTest, fixedwidthDecodeBoolFail) {
auto bytes = ByteArray{0, 1, 2};
ASSERT_OUTCOME_ERROR(decode<ThreeBooleans>(bytes),
DecodeError::UNEXPECTED_VALUE);
}
/**
* @given byte array containing values {0, 1, 2}
* @when scale::decode function is applied sequentially
* @then it returns false, true and kUnexpectedValue error correspondingly,
* and in the end no more bytes left in stream
*/
TEST(ScaleBoolTest, fixedwidthDecodeBoolSuccess) {
auto bytes = ByteArray{0, 1, 0};
ASSERT_OUTCOME_SUCCESS(decoded, decode<ThreeBooleans>(bytes));
ASSERT_EQ(decoded.b1, false);
ASSERT_EQ(decoded.b2, true);
ASSERT_EQ(decoded.b3, false);
}