Created
April 7, 2017 05:56
-
-
Save julianfbeck/f824048b200a4fd32f9bf299e38a0844 to your computer and use it in GitHub Desktop.
sl1
This file contains hidden or 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
| #include <stdlib.h> | |
| #include <printf.h> | |
| #define GRN "\x1B[32m" | |
| #define YEL "\x1B[33m" | |
| #define RESET "\x1B[0m" | |
| typedef enum { | |
| Stop = 0, Start = 1, Finish = 5, Fail = 255 | |
| } Status; | |
| typedef enum { | |
| One = 1, Fifteen = 15, Last = 255 | |
| } Numbers; | |
| typedef struct { | |
| unsigned short int Status; | |
| unsigned short int Numbers; | |
| } TestCase; | |
| unsigned short int switchLowHighByte(unsigned short int i); | |
| void serialize(Status s, Numbers n, unsigned short int *data); | |
| void deserialize(unsigned short int data, Status *s, Numbers *n); | |
| /** | |
| * Vertauscht das low Byte mit dem High Byte | |
| * @param i | |
| * @return | |
| */ | |
| unsigned short int switchLowHighByte(unsigned short int i) { | |
| unsigned short int lowByte = i << 8; | |
| unsigned short int highByte = i >> 8; | |
| return lowByte + highByte; | |
| } | |
| /** | |
| * Speichert den Status Wert in den High Byte von data | |
| * und den Number Wert in den Low Byte | |
| * @param s Status | |
| * @param n Number | |
| * @param data | |
| */ | |
| void serialize(Status s, Numbers n, unsigned short int *data) { | |
| unsigned short int highByte = n << 8; | |
| unsigned short lowByte = s; | |
| *data = highByte | lowByte; | |
| } | |
| /** | |
| * Deserialisiert und speichert den High Byte in Status und den Low Byte von | |
| * data in Numbers | |
| * @param data | |
| * @param s Number | |
| * @param n Status | |
| */ | |
| void deserialize(unsigned short int data, Status *s, Numbers *n) { | |
| *s = (Status) (data & 0xff); | |
| *n = (Numbers) (data >> 8); | |
| } | |
| /** | |
| * Test enum, | |
| */ | |
| enum TestEnum { | |
| OK, FAIL | |
| }; | |
| typedef enum TestEnum Test; | |
| /** | |
| * Test low High function | |
| * @param i | |
| * @return TestEnum | |
| */ | |
| Test testLowHigh(unsigned short int i) { | |
| Test t; | |
| if (i == switchLowHighByte(switchLowHighByte(i))) { | |
| printf("%x", switchLowHighByte(i)); | |
| t = OK; | |
| } else | |
| t = FAIL; | |
| return t; | |
| } | |
| /** | |
| * Serialisiert und Deserialisiert die übergebenen Werte und | |
| * überprüft sie auf gleichheit | |
| * @param s Number | |
| * @param n Status | |
| * @return TestEnum | |
| */ | |
| Test testSD(Status s, Numbers n) { | |
| Test t; | |
| unsigned short int data; | |
| Status s2; | |
| Numbers n2; | |
| serialize(s, n, &data); | |
| printf("Serialized: %X \n",data); | |
| deserialize(data, &s2, &n2); | |
| printf(RESET"Deserialized: Status %i, Number %i", s2,n2); | |
| if (s2 == s && n2 == n) { | |
| t = OK; | |
| } else { | |
| t = FAIL; | |
| } | |
| return t; | |
| } | |
| /** | |
| * Führt die übergebenen TestCases für die testHighlow Function aus | |
| * @param no | |
| * @param test | |
| */ | |
| void runTestsHighLow(int no, TestCase *test) { | |
| Test t; | |
| int i; | |
| for (i = 0; i < no; i++) { | |
| printf(RESET"Test %d für %x: ", i, test[i].Status); | |
| t = testLowHigh(test[i].Status); | |
| if (OK == t) | |
| printf(GRN" OK \n"RESET); | |
| if (FAIL == t) | |
| printf("FAIL \n"); | |
| } | |
| printf("\n"); | |
| printf("\n"); | |
| } | |
| /** | |
| * Führt die übergebenen TestCases für die testSD Function aus | |
| * @param no | |
| * @param test | |
| */ | |
| void runTestsSD(int no, TestCase *test) { | |
| Test t; | |
| int i; | |
| for (i = 0; i < no; i++) { | |
| printf(RESET"Test %d für Status %i, Number %i: \n", i, test[i].Status,test[i].Numbers); | |
| t = testSD((Status) test[i].Status, (Numbers) test[i].Numbers); | |
| if (OK == t) | |
| printf(GRN" OK \n"RESET); | |
| if (FAIL == t) | |
| printf("FAIL \n"); | |
| printf("\n"); | |
| } | |
| } | |
| int main(void) { | |
| /** | |
| * Run Tests für die Switch High LOw Function | |
| */ | |
| printf(YEL"Tests für High Low Bit switch:\n"); | |
| const int testNoHighLow = 4; | |
| TestCase testsHighLow[testNoHighLow] = {{0xaabb}, | |
| {0x1221}, | |
| {0xffdd}, | |
| {0x1100}}; | |
| runTestsHighLow(testNoHighLow, testsHighLow); | |
| /** | |
| * Run tests for serialize and deserialize | |
| */ | |
| printf(YEL"Tests für serialize und deserialize:\n"); | |
| const int testNOSD = 4; | |
| TestCase testsSD[testNOSD] = { | |
| {Start, Fifteen}, | |
| {Stop, One}, | |
| {Finish, Last}, | |
| {FAIL, One} | |
| }; | |
| runTestsSD(testNOSD,testsSD); | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment