1
0

hbase_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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:80/path?query#fragment",
  29. "http://user:pswd@www.example.com/path?query#fragment",
  30. "http://www.example.com/path?query#fragment",
  31. "http://www.example.com/path?query",
  32. "http://www.example.com/path",
  33. "www.example.com/path",
  34. "/path",
  35. };
  36. hurl_t stURL;
  37. for (int i = 0; i < ARRAY_SIZE(test_urls); ++i) {
  38. const char* strURL = test_urls[i];
  39. printf("%s =>\n", strURL);
  40. hv_parse_url(&stURL, strURL);
  41. assert(stURL.port == 80);
  42. // scheme://
  43. if (stURL.fields[HV_URL_SCHEME].len > 0) {
  44. const char* scheme = strURL + stURL.fields[HV_URL_SCHEME].off;
  45. int len = stURL.fields[HV_URL_SCHEME].len;
  46. assert(len == 4);
  47. assert(strncmp(scheme, "http", len) == 0);
  48. printf("%.*s://", len, scheme);
  49. }
  50. // user:pswd@
  51. if (stURL.fields[HV_URL_USERNAME].len > 0) {
  52. const char* user = strURL + stURL.fields[HV_URL_USERNAME].off;
  53. int len = stURL.fields[HV_URL_USERNAME].len;
  54. assert(len == 4);
  55. assert(strncmp(user, "user", len) == 0);
  56. printf("%.*s", len, user);
  57. if (stURL.fields[HV_URL_PASSWORD].len > 0) {
  58. const char* pswd = strURL + stURL.fields[HV_URL_PASSWORD].off;
  59. int len = stURL.fields[HV_URL_PASSWORD].len;
  60. assert(len == 4);
  61. assert(strncmp(pswd, "pswd", len) == 0);
  62. printf(":%.*s", len, pswd);
  63. }
  64. printf("@");
  65. }
  66. // host:port
  67. if (stURL.fields[HV_URL_HOST].len > 0) {
  68. const char* host = strURL + stURL.fields[HV_URL_HOST].off;
  69. int len = stURL.fields[HV_URL_HOST].len;
  70. assert(len == strlen("www.example.com"));
  71. assert(strncmp(host, "www.example.com", len) == 0);
  72. printf("%.*s", len, host);
  73. if (stURL.fields[HV_URL_PORT].len > 0) {
  74. const char* port = strURL + stURL.fields[HV_URL_PORT].off;
  75. int len = stURL.fields[HV_URL_PORT].len;
  76. assert(len == 2);
  77. assert(strncmp(port, "80", len) == 0);
  78. printf(":%.*s", len, port);
  79. }
  80. }
  81. // /path
  82. if (stURL.fields[HV_URL_PATH].len > 0) {
  83. const char* path = strURL + stURL.fields[HV_URL_PATH].off;
  84. int len = stURL.fields[HV_URL_PATH].len;
  85. assert(len == 5);
  86. assert(strncmp(path, "/path", len) == 0);
  87. printf("%.*s", len, path);
  88. }
  89. // ?query
  90. if (stURL.fields[HV_URL_QUERY].len > 0) {
  91. const char* query = strURL + stURL.fields[HV_URL_QUERY].off;
  92. int len = stURL.fields[HV_URL_QUERY].len;
  93. assert(len == 5);
  94. assert(strncmp(query, "query", len) == 0);
  95. printf("?%.*s", len, query);
  96. }
  97. // #fragment
  98. if (stURL.fields[HV_URL_FRAGMENT].len > 0) {
  99. const char* fragment = strURL + stURL.fields[HV_URL_FRAGMENT].off;
  100. int len = stURL.fields[HV_URL_FRAGMENT].len;
  101. assert(len == 8);
  102. assert(strncmp(fragment, "fragment", len) == 0);
  103. printf("#%.*s", len, fragment);
  104. }
  105. printf("\n");
  106. }
  107. return 0;
  108. }