hotspot/test/native/runtime/test_os.cpp
changeset 46587 6c97f34cb194
parent 42056 9296564b8c8b
equal deleted inserted replaced
46569:33bfb73333f9 46587:6c97f34cb194
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
   106     size_t actual = os::page_size_for_region_unaligned(small_page - 17, 1);
   106     size_t actual = os::page_size_for_region_unaligned(small_page - 17, 1);
   107     ASSERT_EQ(small_page, actual);
   107     ASSERT_EQ(small_page, actual);
   108   }
   108   }
   109 }
   109 }
   110 
   110 
       
   111 TEST(os, test_random) {
       
   112   const double m = 2147483647;
       
   113   double mean = 0.0, variance = 0.0, t;
       
   114   const int reps = 10000;
       
   115   unsigned int seed = 1;
       
   116 
       
   117   // tty->print_cr("seed %ld for %ld repeats...", seed, reps);
       
   118   os::init_random(seed);
       
   119   int num;
       
   120   for (int k = 0; k < reps; k++) {
       
   121     num = os::random();
       
   122     double u = (double)num / m;
       
   123     ASSERT_TRUE(u >= 0.0 && u <= 1.0) << "bad random number!";
       
   124 
       
   125     // calculate mean and variance of the random sequence
       
   126     mean += u;
       
   127     variance += (u*u);
       
   128   }
       
   129   mean /= reps;
       
   130   variance /= (reps - 1);
       
   131 
       
   132   ASSERT_EQ(num, 1043618065) << "bad seed";
       
   133   // tty->print_cr("mean of the 1st 10000 numbers: %f", mean);
       
   134   int intmean = mean*100;
       
   135   ASSERT_EQ(intmean, 50);
       
   136   // tty->print_cr("variance of the 1st 10000 numbers: %f", variance);
       
   137   int intvariance = variance*100;
       
   138   ASSERT_EQ(intvariance, 33);
       
   139   const double eps = 0.0001;
       
   140   t = fabsd(mean - 0.5018);
       
   141   ASSERT_LT(t, eps) << "bad mean";
       
   142   t = (variance - 0.3355) < 0.0 ? -(variance - 0.3355) : variance - 0.3355;
       
   143   ASSERT_LT(t, eps) << "bad variance";
       
   144 }
       
   145 
       
   146 
   111 #ifdef ASSERT
   147 #ifdef ASSERT
   112 TEST_VM_ASSERT_MSG(os, page_size_for_region_with_zero_min_pages, "sanity") {
   148 TEST_VM_ASSERT_MSG(os, page_size_for_region_with_zero_min_pages, "sanity") {
   113   size_t region_size = 16 * os::vm_page_size();
   149   size_t region_size = 16 * os::vm_page_size();
   114   os::page_size_for_region_aligned(region_size, 0); // should assert
   150   os::page_size_for_region_aligned(region_size, 0); // should assert
   115 }
   151 }