test.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <signal.h>
  2. #include <ctime>
  3. #include "testlib.h"
  4. extern void run_upstream_tests();
  5. void test_json(const char* string, const char* ret = "") {
  6. TEST_EQ(R::Datum::from_json(string).as_json().c_str(), ret[0] ? ret : string);
  7. }
  8. void test_json_parse_print() {
  9. enter_section("json");
  10. test_json("-0.0", "-0.0");
  11. test_json("null");
  12. test_json("1.2");
  13. test_json("1.2e20", "1.2e+20");
  14. test_json("true");
  15. test_json("false");
  16. test_json("\"\"");
  17. test_json("\"\\u1234\"", "\"\u1234\"");
  18. test_json("\"\\\"\"");
  19. test_json("\"foobar\"");
  20. test_json("[]");
  21. test_json("[1]");
  22. test_json("[1,2,3,4]");
  23. test_json("{}");
  24. test_json("{\"a\":1}");
  25. test_json("{\"a\":1,\"b\":2,\"c\":3}");
  26. exit_section();
  27. }
  28. void test_reql() {
  29. enter_section("reql");
  30. TEST_EQ((R::expr(1) + 2).run(*conn), R::Datum(3));
  31. TEST_EQ(R::range(4).count().run(*conn), R::Datum(4));
  32. TEST_EQ(R::js("Math.abs")(-1).run(*conn), 1);
  33. exit_section();
  34. }
  35. void test_cursor() {
  36. enter_section("cursor");
  37. R::Cursor cursor = R::range(10000).run(*conn);
  38. TEST_EQ(cursor.next(), 0);
  39. R::Array array = cursor.to_array();
  40. TEST_EQ(array.size(), 9999);
  41. TEST_EQ(*array.begin(), 1);
  42. TEST_EQ(*array.rbegin(), 9999);
  43. int i = 0;
  44. R::range(3).run(*conn).each([&i](R::Datum&& datum){
  45. TEST_EQ(datum, i++); });
  46. exit_section();
  47. }
  48. void test_encode(const char* str, const char* b) {
  49. TEST_EQ(R::base64_encode(str), b);
  50. }
  51. void test_decode(const char* b, const char* str) {
  52. std::string out;
  53. TEST_EQ(R::base64_decode(b, out), true);
  54. TEST_EQ(out, str);
  55. }
  56. #define TEST_B64(a, b) test_encode(a, b); test_decode(b, a)
  57. void test_binary() {
  58. enter_section("base64");
  59. TEST_B64("", "");
  60. TEST_B64("foo", "Zm9v");
  61. exit_section();
  62. }
  63. void test_issue28() {
  64. enter_section("issue #28");
  65. std::vector<std::string> expected{ "rethinkdb", "test" };
  66. std::vector<std::string> dbs;
  67. R::Cursor databases = R::db_list().run(*conn);
  68. for (R::Datum const& db : databases) {
  69. dbs.push_back(*db.get_string());
  70. }
  71. TEST_EQ(dbs, expected);
  72. exit_section();
  73. }
  74. int main() {
  75. signal(SIGPIPE, SIG_IGN);
  76. srand(time(NULL));
  77. try {
  78. conn = R::connect();
  79. } catch(const R::Error& error) {
  80. printf("FAILURE: could not connect to localhost:28015: %s\n", error.message.c_str());
  81. return 1;
  82. }
  83. try {
  84. //test_binary();
  85. //test_json_parse_print();
  86. //test_reql();
  87. //test_cursor();
  88. test_issue28();
  89. run_upstream_tests();
  90. } catch (const R::Error& error) {
  91. printf("FAILURE: uncaught expception: %s\n", error.message.c_str());
  92. return 1;
  93. }
  94. if (!failed) {
  95. printf("SUCCESS: %d tests passed\n", count);
  96. } else {
  97. printf("DONE: %d of %d tests failed\n", failed, count);
  98. return 1;
  99. }
  100. }