From 5b5948dee1dec0580e812c7d2d29d722b072ebc1 Mon Sep 17 00:00:00 2001
From: Roman Lebedev <lebedev.ri@gmail.com>
Date: Thu, 27 Jun 2019 21:51:54 +0000
Subject: [PATCH] [NFC][APInt] Add (exhaustive) test for
 multiplicativeInverse()

Else there is no direct test coverage at all.
The function should either return '0' or precise answer.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@364599 91177308-0d34-0410-b5e6-96231b3b80d8
---
 unittests/ADT/APIntTest.cpp | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp
index b69dce1bd8a..0755a8cc667 100644
--- a/unittests/ADT/APIntTest.cpp
+++ b/unittests/ADT/APIntTest.cpp
@@ -2505,4 +2505,21 @@ TEST(APIntTest, SolveQuadraticEquationWrap) {
     Iterate(i);
 }
 
+TEST(APIntTest, MultiplicativeInverseExaustive) {
+  for (unsigned BitWidth = 1; BitWidth <= 16; ++BitWidth) {
+    for (unsigned Value = 0; Value < (1 << BitWidth); ++Value) {
+      APInt V = APInt(BitWidth, Value);
+      APInt MulInv =
+          V.zext(BitWidth + 1)
+              .multiplicativeInverse(APInt::getSignedMinValue(BitWidth + 1))
+              .trunc(BitWidth);
+      APInt One = V * MulInv;
+      EXPECT_TRUE(MulInv.isNullValue() || One.isOneValue())
+          << " bitwidth = " << BitWidth << ", value = " << Value
+          << ", computed multiplicative inverse = " << MulInv
+          << ", value * multiplicative inverse = " << One << " (should be 1)";
+    }
+  }
+}
+
 } // end anonymous namespace
-- 
2.40.0