tqchen commented on a change in pull request #4628: [Object] Add String 
container
URL: https://github.com/apache/incubator-tvm/pull/4628#discussion_r387972834
 
 

 ##########
 File path: tests/cpp/container_test.cc
 ##########
 @@ -221,11 +222,154 @@ TEST(Map, Iterator) {
   using namespace tvm;
   PrimExpr a = 1, b = 2;
   Map<PrimExpr, PrimExpr> map1{{a, b}};
-  std::unordered_map<PrimExpr, PrimExpr, ObjectHash, ObjectEqual>
-      map2(map1.begin(), map1.end());
+  std::unordered_map<PrimExpr, PrimExpr, ObjectHash, ObjectEqual> map2(
+      map1.begin(), map1.end());
   CHECK(map2[a].as<IntImmNode>()->value == 2);
 }
 
+TEST(String, MoveFromStd) {
+  using namespace std;
+  string source = "this is a string";
+  string expect = source;
+  String s(std::move(source));
+  string copy = (string)s;
+  CHECK_EQ(copy, expect);
+  CHECK_EQ(source.size(), 0);
+}
+
+TEST(String, CopyFromStd) {
+  using namespace std;
+  string source = "this is a string";
+  string expect = source;
+  String s{source};
+  string copy = (string)s;
+  CHECK_EQ(copy, expect);
+  CHECK_EQ(source.size(), expect.size());
+}
+
+TEST(String, Assignment) {
+  using namespace std;
+  String s{string{"hello"}};
+  s = string{"world"};
+  CHECK_EQ(s == "world", true);
+  string s2{"world2"};
+  s = std::move(s2);
+  CHECK_EQ(s == "world2", true);
+}
+
+TEST(String, empty) {
+  using namespace std;
+  String s{"hello"};
+  CHECK_EQ(s.empty(), false);
+  s = "";
+  CHECK_EQ(s.empty(), true);
+}
+
+TEST(String, Comparisons) {
+  using namespace std;
+  string source = "a string";
+  string mismatch = "a string but longer";
+  String s{source};
+
+  CHECK_EQ(s == source, true);
+  CHECK_EQ(s == mismatch, false);
+  CHECK_EQ(s == source.data(), true);
+  CHECK_EQ(s == mismatch.data(), false);
+
+  // Check '\0' handling
+  string v1 = "hello world";
+  size_t v1_size = v1.size();
+  v1[5] = '\0';
+  CHECK_EQ(v1[5], '\0');
+  CHECK_EQ(v1.size(), v1_size);
+  String str_v1{v1};
+  CHECK_EQ(str_v1 == v1, true);
+  CHECK_EQ(str_v1.size(), v1_size);
+
+  string v2 = "hello";
+  CHECK_EQ(str_v1 == v2, false);
+}
+
+TEST(String, compare) {
+  using namespace std;
+  string source = "a string";
+  string mismatch1 = "a string but longer";
+  string mismatch2 = "a strin";
+  string mismatch3 = "a b";
+  string mismatch4 = "a t";
+  String str_source{source};
+  String str_mismatch1{mismatch1};
+  String str_mismatch2{mismatch2};
+  String str_mismatch3{mismatch3};
+  String str_mismatch4{mismatch4};
+
+  // compare with string
+  CHECK_EQ(str_source.compare(source), 0);
+  CHECK_LT(str_source.compare(mismatch1), 0);
+  CHECK_GT(str_source.compare(mismatch2), 0);
+  CHECK_GT(str_source.compare(mismatch3), 0);
+  CHECK_LT(str_source.compare(mismatch4), 0);
+
+  // compare with char*
+  CHECK_EQ(str_source.compare(source.data()), 0);
+  CHECK_LT(str_source.compare(mismatch1.data()), 0);
+  CHECK_GT(str_source.compare(mismatch2.data()), 0);
+  CHECK_GT(str_source.compare(mismatch3.data()), 0);
+  CHECK_LT(str_source.compare(mismatch4.data()), 0);
+
+  // compare with String
+  CHECK_LT(str_source.compare(str_mismatch1), 0);
+  CHECK_GT(str_source.compare(str_mismatch2), 0);
+  CHECK_GT(str_source.compare(str_mismatch3), 0);
+  CHECK_LT(str_source.compare(str_mismatch4), 0);
+
+  // Check '\0' handling
+  string v1 = "hello world";
 
 Review comment:
   We need broader coverage for \0 handling. In particular, we should construct 
mismatch cases that would have been equal to each other if we use strncmp

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to