[libcxx-commits] [libcxx] db0ac30 - [libc++] Fix warning C4244 in std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.discrete/eval.pass.cpp

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Aug 2 17:46:45 PDT 2022


Author: Igor Zhukov
Date: 2022-08-02T20:44:46-04:00
New Revision: db0ac307c9df26d26a629552aec0a78f1b492dfd

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

LOG: [libc++] Fix warning C4244 in std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.discrete/eval.pass.cpp

frederick-vs-ja noticed that https://github.com/microsoft/STL/pull/2976#issuecomment-1201926893
while we are working on updating LLVM submodule for MS STL:

    [...]\std\numerics\rand\rand.dist\rand.dist.samp\rand.dist.samp.discrete\eval.pass.cpp(33): error C2220: the following warning is treated as an error
    [...]\std\numerics\rand\rand.dist\rand.dist.samp\rand.dist.samp.discrete\eval.pass.cpp(287): note: see reference to function template instantiation 'void tests<__int64>(void)' being compiled
    [...]\std\numerics\rand\rand.dist\rand.dist.samp\rand.dist.samp.discrete\eval.pass.cpp(33): warning C4244: 'argument': conversion from '__int64' to 'const unsigned int', possible loss of data

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

Added: 
    

Modified: 
    libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.discrete/eval.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.discrete/eval.pass.cpp b/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.discrete/eval.pass.cpp
index d290f51d60c84..d52e23e9dcd4a 100644
--- a/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.discrete/eval.pass.cpp
+++ b/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.discrete/eval.pass.cpp
@@ -30,12 +30,13 @@ void tests() {
         G g;
         D d;
         const int N = 100;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         for (unsigned i = 0; i < u.size(); ++i)
@@ -48,12 +49,13 @@ void tests() {
         double p0[] = {.3};
         D d(p0, p0+1);
         const int N = 100;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         for (unsigned i = 0; i < u.size(); ++i)
@@ -66,12 +68,13 @@ void tests() {
         double p0[] = {.75, .25};
         D d(p0, p0+2);
         const int N = 1000000;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         for (unsigned i = 0; i < u.size(); ++i)
@@ -84,12 +87,13 @@ void tests() {
         double p0[] = {0, 1};
         D d(p0, p0+2);
         const int N = 1000000;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         assert((double)u[0]/N == prob[0]);
@@ -102,12 +106,13 @@ void tests() {
         double p0[] = {1, 0};
         D d(p0, p0+2);
         const int N = 1000000;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         assert((double)u[0]/N == prob[0]);
@@ -120,12 +125,13 @@ void tests() {
         double p0[] = {.3, .1, .6};
         D d(p0, p0+3);
         const int N = 10000000;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         for (unsigned i = 0; i < u.size(); ++i)
@@ -138,12 +144,13 @@ void tests() {
         double p0[] = {0, 25, 75};
         D d(p0, p0+3);
         const int N = 1000000;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         for (unsigned i = 0; i < u.size(); ++i)
@@ -159,12 +166,13 @@ void tests() {
         double p0[] = {25, 0, 75};
         D d(p0, p0+3);
         const int N = 1000000;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         for (unsigned i = 0; i < u.size(); ++i)
@@ -180,12 +188,13 @@ void tests() {
         double p0[] = {25, 75, 0};
         D d(p0, p0+3);
         const int N = 1000000;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         for (unsigned i = 0; i < u.size(); ++i)
@@ -201,12 +210,13 @@ void tests() {
         double p0[] = {0, 0, 1};
         D d(p0, p0+3);
         const int N = 100;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         for (unsigned i = 0; i < u.size(); ++i)
@@ -222,12 +232,13 @@ void tests() {
         double p0[] = {0, 1, 0};
         D d(p0, p0+3);
         const int N = 100;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         for (unsigned i = 0; i < u.size(); ++i)
@@ -243,12 +254,13 @@ void tests() {
         double p0[] = {1, 0, 0};
         D d(p0, p0+3);
         const int N = 100;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         for (unsigned i = 0; i < u.size(); ++i)
@@ -264,12 +276,13 @@ void tests() {
         double p0[] = {33, 0, 0, 67};
         D d(p0, p0+3);
         const int N = 1000000;
-        std::vector<Frequency> u(d.max()+1);
+        std::vector<Frequency> u(static_cast<std::size_t>(d.max()+1));
+        assert(u.max_size() > static_cast<unsigned long long>(d.max()));
         for (int i = 0; i < N; ++i)
         {
             typename D::result_type v = d(g);
             assert(d.min() <= v && v <= d.max());
-            u[v]++;
+            u[static_cast<std::size_t>(v)]++;
         }
         std::vector<double> prob = d.probabilities();
         for (unsigned i = 0; i < u.size(); ++i)


        


More information about the libcxx-commits mailing list