hbase_test.c 4.0 KB

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