hbase_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "hbase.h"
  2. int main(int argc, char* argv[]) {
  3. char buf[16] = {0};
  4. printf("hv_rand(10, 99) -> %d\n", hv_rand(10, 99));
  5. printf("hv_random_string(buf, 10) -> %s\n", hv_random_string(buf, 10));
  6. assert(hv_getboolean("1"));
  7. assert(hv_getboolean("yes"));
  8. assert(hv_wildcard_match("www.example.com", "www.example.com"));
  9. assert(hv_wildcard_match("www.example.com", "*.example.com"));
  10. assert(hv_wildcard_match("www.example.com", "www.*.com"));
  11. assert(hv_wildcard_match("www.example.com", "www.example.*"));
  12. assert(hv_parse_size("256") == 256);
  13. assert(hv_parse_size("1K") == 1024);
  14. assert(hv_parse_size("1G2M3K4B") ==
  15. 1 * 1024 * 1024 * 1024 +
  16. 2 * 1024 * 1024 +
  17. 3 * 1024 +
  18. 4);
  19. assert(hv_parse_time("30") == 30);
  20. assert(hv_parse_time("1m") == 60);
  21. assert(hv_parse_time("1d2h3m4s") ==
  22. 1 * 24 * 60 * 60 +
  23. 2 * 60 * 60 +
  24. 3 * 60 +
  25. 4);
  26. const char* test_urls[] = {
  27. "http://user:pswd@www.example.com:80/path?query#fragment",
  28. "http://user:pswd@www.example.com/path?query#fragment",
  29. "http://www.example.com/path?query#fragment",
  30. "http://www.example.com/path?query",
  31. "http://www.example.com/path",
  32. "www.example.com/path",
  33. "/path",
  34. };
  35. hurl_t stURL;
  36. for (int i = 0; i < ARRAY_SIZE(test_urls); ++i) {
  37. const char* strURL = test_urls[i];
  38. printf("%s =>\n", strURL);
  39. hv_parse_url(&stURL, strURL);
  40. assert(stURL.port == 80);
  41. // scheme://
  42. if (stURL.fields[HV_URL_SCHEME].len > 0) {
  43. const char* scheme = strURL + stURL.fields[HV_URL_SCHEME].off;
  44. int len = stURL.fields[HV_URL_SCHEME].len;
  45. assert(len == 4);
  46. assert(strncmp(scheme, "http", len) == 0);
  47. printf("%.*s://", len, scheme);
  48. }
  49. // user:pswd@
  50. if (stURL.fields[HV_URL_USERNAME].len > 0) {
  51. const char* user = strURL + stURL.fields[HV_URL_USERNAME].off;
  52. int len = stURL.fields[HV_URL_USERNAME].len;
  53. assert(len == 4);
  54. assert(strncmp(user, "user", len) == 0);
  55. printf("%.*s", len, user);
  56. if (stURL.fields[HV_URL_PASSWORD].len > 0) {
  57. const char* pswd = strURL + stURL.fields[HV_URL_PASSWORD].off;
  58. int len = stURL.fields[HV_URL_PASSWORD].len;
  59. assert(len == 4);
  60. assert(strncmp(pswd, "pswd", len) == 0);
  61. printf(":%.*s", len, pswd);
  62. }
  63. printf("@");
  64. }
  65. // host:port
  66. if (stURL.fields[HV_URL_HOST].len > 0) {
  67. const char* host = strURL + stURL.fields[HV_URL_HOST].off;
  68. int len = stURL.fields[HV_URL_HOST].len;
  69. assert(len == strlen("www.example.com"));
  70. assert(strncmp(host, "www.example.com", len) == 0);
  71. printf("%.*s", len, host);
  72. if (stURL.fields[HV_URL_PORT].len > 0) {
  73. const char* port = strURL + stURL.fields[HV_URL_PORT].off;
  74. int len = stURL.fields[HV_URL_PORT].len;
  75. assert(len == 2);
  76. assert(strncmp(port, "80", len) == 0);
  77. printf(":%.*s", len, port);
  78. }
  79. }
  80. // /path
  81. if (stURL.fields[HV_URL_PATH].len > 0) {
  82. const char* path = strURL + stURL.fields[HV_URL_PATH].off;
  83. int len = stURL.fields[HV_URL_PATH].len;
  84. assert(len == 5);
  85. assert(strncmp(path, "/path", len) == 0);
  86. printf("%.*s", len, path);
  87. }
  88. // ?query
  89. if (stURL.fields[HV_URL_QUERY].len > 0) {
  90. const char* query = strURL + stURL.fields[HV_URL_QUERY].off;
  91. int len = stURL.fields[HV_URL_QUERY].len;
  92. assert(len == 5);
  93. assert(strncmp(query, "query", len) == 0);
  94. printf("?%.*s", len, query);
  95. }
  96. // #fragment
  97. if (stURL.fields[HV_URL_FRAGMENT].len > 0) {
  98. const char* fragment = strURL + stURL.fields[HV_URL_FRAGMENT].off;
  99. int len = stURL.fields[HV_URL_FRAGMENT].len;
  100. assert(len == 8);
  101. assert(strncmp(fragment, "fragment", len) == 0);
  102. printf("#%.*s", len, fragment);
  103. }
  104. printf("\n");
  105. }
  106. return 0;
  107. }