test.c 2.7 KB

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