test.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 testGetId()
  29. {
  30. unsigned long int id;
  31. BYTE_BUFFER buffer;
  32. char bytes[] = {0x28, 0x29, 0x01, 0x9a};
  33. TEST_START;
  34. buffer = createByteBuffer();
  35. pushBytes(&buffer, bytes, 4);
  36. id = getHomeEasyId(buffer);
  37. printf("Should 00A0A406\nGet %08X\n", id);
  38. }
  39. void testEncode()
  40. {
  41. unsigned char srcByte = 0x28;
  42. unsigned short int encWord;
  43. TEST_START;
  44. encWord = encodeByte(srcByte);
  45. printf("Input: %02X\nShould: 0x5995\nGet: %04X\n", srcByte, encWord);
  46. }
  47. void testDecode()
  48. {
  49. unsigned short int srcWord = 0x5995;
  50. unsigned char decByte;
  51. TEST_START;
  52. decByte = decodeByte(srcWord);
  53. printf("Input: %04X\nShould: 0x28\nGet: %02X\n", srcWord, decByte);
  54. }
  55. void testHomeEasyEncode()
  56. {
  57. BYTE_BUFFER source;
  58. BYTE_BUFFER encoded;
  59. unsigned char bytes[] = {0x28, 0x28, 0x01};
  60. TEST_START;
  61. source = createByteBuffer();
  62. pushBytes(&source, bytes, 3);
  63. printf("Input: ");
  64. printfByteBuffer(source);
  65. printf("Should: 59 95 59 95 55 56\nGet: ");
  66. encoded = homeEasyEncode(&source);
  67. printfByteBuffer(encoded);
  68. destroyByteBuffer(source);
  69. destroyByteBuffer(encoded);
  70. }
  71. void testHomeEasyDecode()
  72. {
  73. BYTE_BUFFER source;
  74. BYTE_BUFFER decoded;
  75. unsigned char bytes[] = {0x59, 0x95, 0x59, 0x95, 0x55, 0x56};
  76. TEST_START;
  77. source = createByteBuffer();
  78. pushBytes(&source, bytes, 6);
  79. printf("Input: ");
  80. printfByteBuffer(source);
  81. printf("Should: 28 28 01\nGet: ");
  82. decoded = homeEasyDecode(&source);
  83. printfByteBuffer(decoded);
  84. destroyByteBuffer(source);
  85. destroyByteBuffer(decoded);
  86. }
  87. void testHomeEasyCommand(char section, unsigned char num, unsigned char onOff)
  88. {
  89. BYTE_BUFFER command;
  90. BYTE_BUFFER encoded;
  91. unsigned char id[] = {0x28, 0x28, 0x01, 0x80};
  92. TEST_START;
  93. command = createHomeEasyCommand(id, section, num, onOff);
  94. printfByteBuffer(command);
  95. encoded = homeEasyEncode(&command);
  96. printfByteBuffer(encoded);
  97. destroyByteBuffer(command);
  98. destroyByteBuffer(encoded);
  99. }
  100. int main()
  101. {
  102. printf("Test\n");
  103. testByteBuffer();
  104. testPrintBits();
  105. testEncode();
  106. testDecode();
  107. testHomeEasyEncode();
  108. testHomeEasyDecode();
  109. testHomeEasyCommand('D', 4, OFF);
  110. testGetId();
  111. return 0;
  112. }