-
Notifications
You must be signed in to change notification settings - Fork 48
/
TestHamming.cpp
71 lines (60 loc) · 1.97 KB
/
TestHamming.cpp
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
67
68
69
70
71
// Copyright (c) 2016-2016 Lime Microsystems
// SPDX-License-Identifier: BSL-1.0
#include <Pothos/Testing.hpp>
#include <iostream>
#include "LoRaCodes.hpp"
POTHOS_TEST_BLOCK("/lora/tests", test_hamming84)
{
bool error;
unsigned char decoded;
//test hamming 84 with bit errors
for (size_t i = 0; i < 16; i++)
{
unsigned char byte = i & 0xff;
unsigned char encoded = encodeHamming84sx(byte);
//check no bit errors
error = false;
decoded = decodeHamming84sx(encoded, error);
POTHOS_TEST_TRUE(not error);
POTHOS_TEST_EQUAL(byte, decoded);
for (int bit0 = 0; bit0 < 8; bit0++)
{
//check 1 bit error
error = false;
unsigned char encoded1err = encoded ^ (1 << bit0);
decoded = decodeHamming84sx(encoded1err, error);
POTHOS_TEST_TRUE(not error);
POTHOS_TEST_EQUAL(byte, decoded);
for (int bit1 = 0; bit1 < 8; bit1++)
{
if (bit1 == bit0) continue;
//check 2 bit errors (cant correct, but can detect
error = false;
unsigned char encoded2err = encoded1err ^ (1 << bit1);
decoded = decodeHamming84sx(encoded2err, error);
POTHOS_TEST_TRUE(error);
}
}
}
}
POTHOS_TEST_BLOCK("/lora/tests", test_hamming74)
{
bool error;
unsigned char decoded;
//test hamming 74 with bit errors
for (size_t i = 0; i < 16; i++)
{
unsigned char byte = i & 0xff;
unsigned char encoded = encodeHamming74sx(byte);
//check no bit errors
decoded = decodeHamming74sx(encoded);
POTHOS_TEST_EQUAL(byte, decoded);
for (int bit0 = 0; bit0 < 8; bit0++)
{
//check 1 bit error
unsigned char encoded1err = encoded ^ (1 << bit0);
decoded = decodeHamming74sx(encoded1err);
POTHOS_TEST_EQUAL(byte, decoded);
}
}
}