Smaller buffers, added full codec test.

This commit is contained in:
Francois Best 2016-10-05 05:52:04 -07:00
parent 8b5e81ed86
commit aeffb7d7c5
1 changed files with 44 additions and 22 deletions

View File

@ -16,15 +16,13 @@ TEST(SysExCodec, Encoder)
// ASCII content // ASCII content
{ {
const byte input[] = "Hello, World!"; const byte input[] = "Hello, World!";
byte buffer[32]; byte buffer[16];
memset(buffer, 0, 32 * sizeof(byte)); memset(buffer, 0, 16 * sizeof(byte));
const unsigned encodedSize = midi::encodeSysEx(input, buffer, 13); const unsigned encodedSize = midi::encodeSysEx(input, buffer, 13);
EXPECT_EQ(encodedSize, 15); EXPECT_EQ(encodedSize, 15);
const byte expected[32] = { const byte expected[16] = {
0, 'H', 'e', 'l', 'l', 'o', ',', ' ', 0, 'H', 'e', 'l', 'l', 'o', ',', ' ',
0, 'W', 'o', 'r', 'l', 'd', '!', 0, 0, 'W', 'o', 'r', 'l', 'd', '!', 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
}; };
EXPECT_THAT(buffer, Each(Le(0x7f))); // All elements are <= 127 EXPECT_THAT(buffer, Each(Le(0x7f))); // All elements are <= 127
EXPECT_THAT(buffer, ContainerEq(expected)); EXPECT_THAT(buffer, ContainerEq(expected));
@ -35,16 +33,14 @@ TEST(SysExCodec, Encoder)
182, 236, 167, 177, 61, 91, 120, // 01111000 -> 120 182, 236, 167, 177, 61, 91, 120, // 01111000 -> 120
107, 94, 209, 87, 94 // 000100xx -> 16 107, 94, 209, 87, 94 // 000100xx -> 16
}; };
byte buffer[32]; byte buffer[16];
memset(buffer, 0, 32 * sizeof(byte)); memset(buffer, 0, 16 * sizeof(byte));
const unsigned encodedSize = midi::encodeSysEx(input, buffer, 12); const unsigned encodedSize = midi::encodeSysEx(input, buffer, 12);
EXPECT_EQ(encodedSize, 14); EXPECT_EQ(encodedSize, 14);
const byte expected[32] = { const byte expected[16] = {
// MSB Data // MSB Data
120, 54, 108, 39, 49, 61, 91, 120, 120, 54, 108, 39, 49, 61, 91, 120,
16, 107, 94, 81, 87, 94, 0, 0, 16, 107, 94, 81, 87, 94, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
}; };
EXPECT_THAT(buffer, Each(Le(0x7f))); // All elements are <= 127 EXPECT_THAT(buffer, Each(Le(0x7f))); // All elements are <= 127
EXPECT_THAT(buffer, ContainerEq(expected)); EXPECT_THAT(buffer, ContainerEq(expected));
@ -59,15 +55,13 @@ TEST(SysExCodec, Decoder)
0, 'H', 'e', 'l', 'l', 'o', ',', ' ', 0, 'H', 'e', 'l', 'l', 'o', ',', ' ',
0, 'W', 'o', 'r', 'l', 'd', '!', 0, 'W', 'o', 'r', 'l', 'd', '!',
}; };
byte buffer[32]; byte buffer[16];
memset(buffer, 0, 32 * sizeof(byte)); memset(buffer, 0, 16 * sizeof(byte));
const unsigned decodedSize = midi::decodeSysEx(input, buffer, 15); const unsigned decodedSize = midi::decodeSysEx(input, buffer, 15);
EXPECT_EQ(decodedSize, 13); EXPECT_EQ(decodedSize, 13);
const byte expected[32] = { const byte expected[16] = {
'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W',
'o', 'r', 'l', 'd', '!', 0, 0, 0, 'o', 'r', 'l', 'd', '!', 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
}; };
EXPECT_THAT(buffer, Each(Le(0x7f))); // All elements are <= 127 EXPECT_THAT(buffer, Each(Le(0x7f))); // All elements are <= 127
EXPECT_THAT(buffer, ContainerEq(expected)); EXPECT_THAT(buffer, ContainerEq(expected));
@ -79,16 +73,14 @@ TEST(SysExCodec, Decoder)
120, 54, 108, 39, 49, 61, 91, 120, 120, 54, 108, 39, 49, 61, 91, 120,
16, 107, 94, 81, 87, 94, 16, 107, 94, 81, 87, 94,
}; };
byte buffer[32]; byte buffer[16];
memset(buffer, 0, 32 * sizeof(byte)); memset(buffer, 0, 16 * sizeof(byte));
const unsigned encodedSize = midi::decodeSysEx(input, buffer, 14); const unsigned encodedSize = midi::decodeSysEx(input, buffer, 14);
EXPECT_EQ(encodedSize, 12); EXPECT_EQ(encodedSize, 12);
const byte expected[32] = { const byte expected[16] = {
182, 236, 167, 177, 61, 91, 120, 182, 236, 167, 177, 61, 91, 120,
107, 94, 209, 87, 94, 0, 0, 107, 94, 209, 87, 94, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
}; };
EXPECT_THAT(input, Each(Le(0x7f))); // All elements are <= 127 EXPECT_THAT(input, Each(Le(0x7f))); // All elements are <= 127
EXPECT_THAT(buffer, ContainerEq(expected)); EXPECT_THAT(buffer, ContainerEq(expected));
@ -97,7 +89,37 @@ TEST(SysExCodec, Decoder)
TEST(SysExCodec, Codec) TEST(SysExCodec, Codec)
{ {
// ASCII content
{
const byte input[] = "Hello, World!";
byte buffer1[16];
byte buffer2[16];
memset(buffer1, 0, 16 * sizeof(byte));
memset(buffer2, 0, 16 * sizeof(byte));
const unsigned encodedSize = midi::encodeSysEx(input, buffer1, 13);
EXPECT_EQ(encodedSize, 15);
const unsigned decodedSize = midi::decodeSysEx(buffer1, buffer2, encodedSize);
EXPECT_EQ(decodedSize, 13);
EXPECT_STREQ(reinterpret_cast<const char*>(buffer2),
reinterpret_cast<const char*>(input));
}
// Non-ASCII content
{
const byte input[] = {
// MSB Data
182, 236, 167, 177, 61, 91, 120,
107, 94, 209, 87, 94
};
byte buffer1[14];
byte buffer2[12];
memset(buffer1, 0, 14 * sizeof(byte));
memset(buffer2, 0, 12 * sizeof(byte));
const unsigned encodedSize = midi::encodeSysEx(input, buffer1, 12);
EXPECT_EQ(encodedSize, 14);
const unsigned decodedSize = midi::decodeSysEx(buffer1, buffer2, encodedSize);
EXPECT_EQ(decodedSize, 12);
EXPECT_THAT(buffer2, ContainerEq(input));
}
} }
END_UNNAMED_NAMESPACE END_UNNAMED_NAMESPACE