test.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <stdio.h>
  2. #include "../home_easy.h"
  3. #include "../buffer.h"
  4. #define TEST_START printf("\n=== %s ===\n", __FUNCTION__)
  5. void testByteBuffer()
  6. {
  7. BYTE_BUFFER buffer;
  8. char bytes[] = {0xcd, 0xef, 0x12};
  9. TEST_START;
  10. buffer = createByteBuffer();
  11. pushByte(&buffer, 0xab);
  12. pushBytes(&buffer, bytes, 3);
  13. pushWord(&buffer, 0x3456);
  14. printf("Should: AB CD EF 12 34 56\nGet: ");
  15. printfByteBuffer(buffer);
  16. destroyByteBuffer(buffer);
  17. }
  18. void testPrintBits()
  19. {
  20. BYTE_BUFFER buffer;
  21. char bytes[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
  22. TEST_START;
  23. buffer = createByteBuffer();
  24. pushBytes(&buffer, bytes, 8);
  25. printfBitBuffer(buffer);
  26. destroyByteBuffer(buffer);
  27. }
  28. void testEncode()
  29. {
  30. unsigned char srcByte = 0x28;
  31. unsigned short int encWord;
  32. TEST_START;
  33. encWord = encodeByte(srcByte);
  34. printf("Input: %02X\nShould: 0x5995\nGet: %04X\n", srcByte, encWord);
  35. }
  36. void testDecode()
  37. {
  38. unsigned short int srcWord = 0x5995;
  39. unsigned char decByte;
  40. TEST_START;
  41. decByte = decodeByte(srcWord);
  42. printf("Input: %04X\nShould: 0x28\nGet: %02X\n", srcWord, decByte);
  43. }
  44. void testHomeEasyEncode()
  45. {
  46. BYTE_BUFFER source;
  47. BYTE_BUFFER encoded;
  48. unsigned char bytes[] = {0x28, 0x28, 0x01};
  49. TEST_START;
  50. source = createByteBuffer();
  51. pushBytes(&source, bytes, 3);
  52. printf("Input: ");
  53. printfByteBuffer(source);
  54. printf("Should: 59 95 59 95 55 56\nGet: ");
  55. encoded = homeEasyEncode(&source);
  56. printfByteBuffer(encoded);
  57. destroyByteBuffer(source);
  58. destroyByteBuffer(encoded);
  59. }
  60. void testHomeEasyDecode()
  61. {
  62. BYTE_BUFFER source;
  63. BYTE_BUFFER decoded;
  64. unsigned char bytes[] = {0x59, 0x95, 0x59, 0x95, 0x55, 0x56};
  65. TEST_START;
  66. source = createByteBuffer();
  67. pushBytes(&source, bytes, 6);
  68. printf("Input: ");
  69. printfByteBuffer(source);
  70. printf("Should: 28 28 01\nGet: ");
  71. decoded = homeEasyDecode(&source);
  72. printfByteBuffer(decoded);
  73. destroyByteBuffer(source);
  74. destroyByteBuffer(decoded);
  75. }
  76. void testHomeEasyCommand(char section, unsigned char num, unsigned char onOff)
  77. {
  78. BYTE_BUFFER command;
  79. BYTE_BUFFER encoded;
  80. unsigned char id[] = {0x28, 0x28, 0x01, 0x80};
  81. TEST_START;
  82. command = createHomeEasyCommand(id, section, num, onOff);
  83. printfByteBuffer(command);
  84. encoded = homeEasyEncode(&command);
  85. printfByteBuffer(encoded);
  86. destroyByteBuffer(command);
  87. destroyByteBuffer(encoded);
  88. }
  89. int main()
  90. {
  91. printf("Test\n");
  92. testByteBuffer();
  93. testPrintBits();
  94. testEncode();
  95. testDecode();
  96. testHomeEasyEncode();
  97. testHomeEasyDecode();
  98. testHomeEasyCommand('D', 4, OFF);
  99. return 0;
  100. }