hash_match_demo.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2022, smartmx <smartmx@qq.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-04-03 smartmx the first version
  9. *
  10. */
  11. #include "hash-match.h"
  12. #include <rtthread.h>
  13. /* test1 */
  14. void hash_match_test1func(void *t)
  15. {
  16. rt_kprintf("test1\n");
  17. }
  18. const uint8_t hash_match_test1key[] = {'a', 'b', 'c', 'd', 'e', 'f', 232, 'g', 168, 192};
  19. /* test upper and lower compare. */
  20. const uint8_t hash_match_test1keyu[] = {'A', 'B', 'C', 'd', 'e', 'f', 232, 'g', 168, 192};
  21. HASH_MATCH_EXPORT(hash_match_test, hash_match_test1, hash_match_test1key, sizeof(hash_match_test1key), hash_match_test1func, "this is test 1");
  22. /* different section group with same key, for test. */
  23. void hash_match_test1func1(void *t)
  24. {
  25. rt_kprintf("test11\n");
  26. }
  27. HASH_MATCH_EXPORT(hash_match_test1, hash_match_test11, hash_match_test1key, sizeof(hash_match_test1key), hash_match_test1func1, "this is test 11");
  28. /* test2 */
  29. void hash_match_test2func(void *t)
  30. {
  31. rt_kprintf("test2\n");
  32. }
  33. const uint8_t hash_match_test2key[] = {51, 135, 10, 0, 33, 67, 45, 123, 172, 8, 0};
  34. HASH_MATCH_EXPORT(hash_match_test, hash_match_test2, hash_match_test2key, sizeof(hash_match_test2key), hash_match_test2func, "this is test 2");
  35. /* different section group with same key, for test. */
  36. void hash_match_test2func1(void *t)
  37. {
  38. rt_kprintf("test21\n");
  39. }
  40. HASH_MATCH_EXPORT(hash_match_test1, hash_match_test21, hash_match_test2key, sizeof(hash_match_test2key), hash_match_test2func1, "this is test 21");
  41. /* test3 */
  42. void hash_match_test3func(void *t)
  43. {
  44. rt_kprintf("test3\n");
  45. }
  46. const uint8_t hash_match_test3key[] = {8, 99, 23, 170, 234, 7, 212, 65, 20, 88, 19, 122};
  47. HASH_MATCH_EXPORT(hash_match_test, hash_match_test3, hash_match_test3key, sizeof(hash_match_test3key), hash_match_test3func, "this is test 3");
  48. /* different section group with same key, for test. */
  49. void hash_match_test3func1(void *t)
  50. {
  51. rt_kprintf("test31\n");
  52. }
  53. HASH_MATCH_EXPORT(hash_match_test1, hash_match_test31, hash_match_test3key, sizeof(hash_match_test3key), hash_match_test3func1, "this is test 31");
  54. static void hash_match_test_task(void *arg)
  55. {
  56. /* init hash_match_test section group. */
  57. HASH_MATCH_INIT(hash_match_test);
  58. HASH_MATCH_LIST(hash_match_test);
  59. /* init hash_match_test1 section group. */
  60. HASH_MATCH_INIT(hash_match_test1);
  61. HASH_MATCH_LIST(hash_match_test1);
  62. while (1)
  63. {
  64. HASH_MATCH(hash_match_test, hash_match_test1key, sizeof(hash_match_test1key), NULL);
  65. HASH_MATCH(hash_match_test, hash_match_test2key, sizeof(hash_match_test2key), NULL);
  66. HASH_MATCH(hash_match_test, hash_match_test3key, sizeof(hash_match_test3key), NULL);
  67. /* test upper and lower compare. */
  68. HASH_MATCH(hash_match_test, hash_match_test1keyu, sizeof(hash_match_test1keyu), NULL);
  69. /* mix up the length, to try. */
  70. HASH_MATCH(hash_match_test, hash_match_test1key, sizeof(hash_match_test2key), NULL);
  71. HASH_MATCH(hash_match_test, hash_match_test2key, sizeof(hash_match_test3key), NULL);
  72. HASH_MATCH(hash_match_test, hash_match_test3key, sizeof(hash_match_test1key), NULL);
  73. /* try with different section group. */
  74. HASH_MATCH(hash_match_test1, hash_match_test1key, sizeof(hash_match_test1key), NULL);
  75. HASH_MATCH(hash_match_test1, hash_match_test2key, sizeof(hash_match_test2key), NULL);
  76. HASH_MATCH(hash_match_test1, hash_match_test3key, sizeof(hash_match_test3key), NULL);
  77. rt_thread_mdelay(1000);
  78. }
  79. }
  80. int hash_match_test_main(void)
  81. {
  82. rt_thread_t tid = RT_NULL;
  83. /* Create background ticks thread */
  84. tid = rt_thread_create("hash-match", hash_match_test_task, RT_NULL, 1024, 10, 10);
  85. if (tid != RT_NULL)
  86. {
  87. rt_thread_startup(tid);
  88. }
  89. return 0;
  90. }
  91. INIT_APP_EXPORT(hash_match_test_main);