[libc-commits] [libc] 2fc1913 - [libc] Check signs instead of values in memcmp unittests.

Cheng Wang via libc-commits libc-commits at lists.llvm.org
Fri Sep 10 03:35:19 PDT 2021


Author: Cheng Wang
Date: 2021-09-10T18:35:10+08:00
New Revision: 2fc1913505e3d5f48b3d5d84ce372a34e661fc7e

URL: https://github.com/llvm/llvm-project/commit/2fc1913505e3d5f48b3d5d84ce372a34e661fc7e
DIFF: https://github.com/llvm/llvm-project/commit/2fc1913505e3d5f48b3d5d84ce372a34e661fc7e.diff

LOG: [libc] Check signs instead of values in memcmp unittests.

The C standard only guarantees the sign of return value. The exact return
value is implementation defined.

Reviewed By: gchatelet

Differential Revision: https://reviews.llvm.org/D109588

Added: 
    

Modified: 
    libc/test/src/string/memcmp_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/test/src/string/memcmp_test.cpp b/libc/test/src/string/memcmp_test.cpp
index 9be1e56302f8f..35ee741514aa5 100644
--- a/libc/test/src/string/memcmp_test.cpp
+++ b/libc/test/src/string/memcmp_test.cpp
@@ -11,7 +11,7 @@
 
 TEST(LlvmLibcMemcmpTest, CmpZeroByte) {
   const char *lhs = "ab";
-  const char *rhs = "bc";
+  const char *rhs = "yz";
   EXPECT_EQ(__llvm_libc::memcmp(lhs, rhs, 0), 0);
 }
 
@@ -23,14 +23,14 @@ TEST(LlvmLibcMemcmpTest, LhsRhsAreTheSame) {
 
 TEST(LlvmLibcMemcmpTest, LhsBeforeRhsLexically) {
   const char *lhs = "ab";
-  const char *rhs = "ac";
-  EXPECT_EQ(__llvm_libc::memcmp(lhs, rhs, 2), -1);
+  const char *rhs = "az";
+  EXPECT_LT(__llvm_libc::memcmp(lhs, rhs, 2), 0);
 }
 
 TEST(LlvmLibcMemcmpTest, LhsAfterRhsLexically) {
-  const char *lhs = "ac";
+  const char *lhs = "az";
   const char *rhs = "ab";
-  EXPECT_EQ(__llvm_libc::memcmp(lhs, rhs, 2), 1);
+  EXPECT_GT(__llvm_libc::memcmp(lhs, rhs, 2), 0);
 }
 
 TEST(LlvmLibcMemcmpTest, Sweep) {
@@ -51,8 +51,8 @@ TEST(LlvmLibcMemcmpTest, Sweep) {
   reset(lhs);
   reset(rhs);
   for (size_t i = 0; i < kMaxSize; ++i) {
-    rhs[i] = 'b';
-    EXPECT_EQ(__llvm_libc::memcmp(lhs, rhs, kMaxSize), -1);
+    rhs[i] = 'z';
+    EXPECT_LT(__llvm_libc::memcmp(lhs, rhs, kMaxSize), 0);
     rhs[i] = 'a';
   }
 }


        


More information about the libc-commits mailing list