util_test.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SExp - A S-Expression Parser for C++
  2. // Copyright (C) 2015 Ingo Ruhnke <grumbel@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include <gtest/gtest.h>
  17. #include <sstream>
  18. #include "sexp/parser.hpp"
  19. #include "sexp/util.hpp"
  20. #include "sexp/value.hpp"
  21. TEST(UtilTest, is_list)
  22. {
  23. EXPECT_TRUE(sexp::is_list(sexp::Parser::from_string("()")));
  24. EXPECT_TRUE(sexp::is_list(sexp::Parser::from_string("(1)")));
  25. EXPECT_TRUE(sexp::is_list(sexp::Parser::from_string("(1 2)")));
  26. EXPECT_TRUE(sexp::is_list(sexp::Parser::from_string("(1 2 3)")));
  27. EXPECT_TRUE(sexp::is_list(sexp::Parser::from_string("((1) (2 3) (3 4 5))")));
  28. EXPECT_TRUE(sexp::is_list(sexp::Parser::from_string("(1 . (2 . (3 . (4 . (5 . ())))))")));
  29. EXPECT_FALSE(sexp::is_list(sexp::Parser::from_string("(1 . 5)")));
  30. EXPECT_FALSE(sexp::is_list(sexp::Parser::from_string("(1 2 3 4 . 5)")));
  31. EXPECT_FALSE(sexp::is_list(sexp::Parser::from_string("(1 . (2 . (3 . 5)))")));
  32. }
  33. TEST(UtilTest, list_iterator)
  34. {
  35. sexp::Value lst = sexp::Parser::from_string("(1 2 3 4 5 6 7 8 9)");
  36. std::vector<int> result;
  37. for(sexp::ListIterator it(lst); it != sexp::ListIterator(); ++it)
  38. {
  39. result.push_back(it->as_int());
  40. }
  41. std::vector<int> expected{1, 2, 3, 4, 5, 6, 7, 8, 9};
  42. ASSERT_EQ(expected, result);
  43. }
  44. TEST(UtilTest, list_iterator_invalid)
  45. {
  46. // the list isn't terminated with (), but with 10, it should get silently ignored
  47. sexp::Value lst = sexp::Parser::from_string("(1 2 3 4 5 6 7 8 9 . 10)");
  48. std::vector<int> result;
  49. for(sexp::ListIterator it(lst); it != sexp::ListIterator(); ++it)
  50. {
  51. result.push_back(it->as_int());
  52. }
  53. std::vector<int> expected{1, 2, 3, 4, 5, 6, 7, 8, 9};
  54. ASSERT_EQ(expected, result);
  55. }
  56. TEST(UtilTest, list_iterator_nil)
  57. {
  58. sexp::Value sx = sexp::Value::nil();
  59. for(sexp::ListIterator it(sx); it != sexp::ListIterator(); ++it)
  60. {
  61. (void)it;
  62. ASSERT_TRUE(false);
  63. }
  64. }
  65. TEST(UtilTest, list_adapter)
  66. {
  67. sexp::Value lst = sexp::Parser::from_string("(1 2 3 4 5 6 7 8 9)");
  68. std::vector<int> result;
  69. for(auto const& sx : sexp::ListAdapter(lst))
  70. {
  71. result.push_back(sx.as_int());
  72. }
  73. std::vector<int> expected{1, 2, 3, 4, 5, 6, 7, 8, 9};
  74. ASSERT_EQ(expected, result);
  75. }
  76. TEST(UtilTest, list_adapter_nil)
  77. {
  78. sexp::Value lst = sexp::Value::nil();
  79. for(auto const& sx : sexp::ListAdapter(lst))
  80. {
  81. (void)sx;
  82. ASSERT_TRUE(false);
  83. }
  84. }
  85. TEST(UtilTest, list_length)
  86. {
  87. sexp::Value lst = sexp::Parser::from_string("(1 2 3 4 5 6 7 8 9)");
  88. ASSERT_EQ(9, sexp::list_length(lst));
  89. sexp::Value lst_invalid = sexp::Parser::from_string("(1 2 3 4 5 6 7 8 9 . 10)");
  90. ASSERT_THROW(sexp::list_length(lst_invalid), sexp::TypeError);
  91. sexp::Value lst_nil = sexp::Value::nil();
  92. int expected = 0;
  93. ASSERT_EQ(expected, sexp::list_length(lst_nil));
  94. }
  95. TEST(UtilTest, list_ref)
  96. {
  97. sexp::Value lst = sexp::Parser::from_string("(5 4 3 2 1)");
  98. ASSERT_EQ(5, sexp::list_ref(lst, 0).as_int());
  99. ASSERT_EQ(4, sexp::list_ref(lst, 1).as_int());
  100. ASSERT_EQ(3, sexp::list_ref(lst, 2).as_int());
  101. ASSERT_EQ(2, sexp::list_ref(lst, 3).as_int());
  102. ASSERT_EQ(1, sexp::list_ref(lst, 4).as_int());
  103. }
  104. TEST(UtilTest, assoc_ref)
  105. {
  106. sexp::Value alist = sexp::Parser::from_string("((foo . 1) (bar . 2) (baz . 3))");
  107. ASSERT_EQ(1, sexp::assoc_ref(alist, "foo").as_int());
  108. ASSERT_EQ(2, sexp::assoc_ref(alist, "bar").as_int());
  109. ASSERT_EQ(3, sexp::assoc_ref(alist, "baz").as_int());
  110. ASSERT_EQ(sexp::Value::nil(), sexp::assoc_ref(alist, "foobar"));
  111. }
  112. /* EOF */