-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2edd963
commit fa75870
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <assert.h> | ||
#include <stdint.h> | ||
#include <unistd.h> | ||
|
||
void test_swab_even_bytes() { | ||
uint8_t input[] = {0x01, 0x02, 0x03, 0x04}; | ||
uint8_t output[4]; | ||
|
||
swab(input, output, sizeof(input)); | ||
|
||
assert(output[0] == 0x02); | ||
assert(output[1] == 0x01); | ||
assert(output[2] == 0x04); | ||
assert(output[3] == 0x03); | ||
} | ||
|
||
void test_swab_odd_bytes() { | ||
uint8_t input[] = {0x01, 0x02, 0x03, 0x04, 0x05}; | ||
uint8_t output[5]; | ||
|
||
swab(input, output, sizeof(input)); | ||
|
||
assert(output[0] == 0x02); | ||
assert(output[1] == 0x01); | ||
assert(output[2] == 0x04); | ||
assert(output[3] == 0x03); | ||
|
||
// Last byte is UB, assume unchanged? | ||
assert(output[4] == 0x05); | ||
} | ||
|
||
void test_swab_negative_bytes() { | ||
uint8_t input[] = {0x01, 0x02, 0x03, 0x04}; | ||
uint8_t output[4]; | ||
|
||
swab(input, output, -1); // Should change nothing | ||
|
||
assert(output[0] == 0x01); | ||
assert(output[1] == 0x02); | ||
assert(output[2] == 0x03); | ||
assert(output[3] == 0x04); | ||
} | ||
|
||
void test_swab_zero_bytes() { | ||
uint8_t input[] = {0x01, 0x02, 0x03, 0x04}; | ||
uint8_t output[4]; | ||
|
||
swab(input, output, 0); // Should change nothing | ||
|
||
assert(output[0] == 0x01); | ||
assert(output[1] == 0x02); | ||
assert(output[2] == 0x03); | ||
assert(output[3] == 0x04); | ||
} | ||
|
||
int main() { | ||
test_swab_even_bytes(); | ||
test_swab_odd_bytes(); | ||
test_swab_negative_bytes(); | ||
test_swab_zero_bytes(); | ||
|
||
return 0; | ||
} |