Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: test/cctest/test-strtod.cc

Issue 23548024: Introduce a RandonNumberGenerator class. Refactor the random/private_random uses in Isolate/Context. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: REBASE Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/cctest/test-strings.cc ('k') | test/cctest/test-utils.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 } 425 }
426 426
427 427
428 // Copied from v8.cc and adapted to make the function deterministic. 428 // Copied from v8.cc and adapted to make the function deterministic.
429 static uint32_t DeterministicRandom() { 429 static uint32_t DeterministicRandom() {
430 // Random number generator using George Marsaglia's MWC algorithm. 430 // Random number generator using George Marsaglia's MWC algorithm.
431 static uint32_t hi = 0; 431 static uint32_t hi = 0;
432 static uint32_t lo = 0; 432 static uint32_t lo = 0;
433 433
434 // Initialization values don't have any special meaning. (They are the result 434 // Initialization values don't have any special meaning. (They are the result
435 // of two calls to random().) 435 // of two calls to rand().)
436 if (hi == 0) hi = 0xbfe166e7; 436 if (hi == 0) hi = 0xbfe166e7;
437 if (lo == 0) lo = 0x64d1c3c9; 437 if (lo == 0) lo = 0x64d1c3c9;
438 438
439 // Mix the bits. 439 // Mix the bits.
440 hi = 36969 * (hi & 0xFFFF) + (hi >> 16); 440 hi = 36969 * (hi & 0xFFFF) + (hi >> 16);
441 lo = 18273 * (lo & 0xFFFF) + (lo >> 16); 441 lo = 18273 * (lo & 0xFFFF) + (lo >> 16);
442 return (hi << 16) + (lo & 0xFFFF); 442 return (hi << 16) + (lo & 0xFFFF);
443 } 443 }
444 444
445 445
446 static const int kBufferSize = 1024; 446 static const int kBufferSize = 1024;
447 static const int kShortStrtodRandomCount = 2; 447 static const int kShortStrtodRandomCount = 2;
448 static const int kLargeStrtodRandomCount = 2; 448 static const int kLargeStrtodRandomCount = 2;
449 449
450 TEST(RandomStrtod) { 450 TEST(RandomStrtod) {
451 srand(time(NULL));
451 char buffer[kBufferSize]; 452 char buffer[kBufferSize];
452 for (int length = 1; length < 15; length++) { 453 for (int length = 1; length < 15; length++) {
453 for (int i = 0; i < kShortStrtodRandomCount; ++i) { 454 for (int i = 0; i < kShortStrtodRandomCount; ++i) {
454 int pos = 0; 455 int pos = 0;
455 for (int j = 0; j < length; ++j) { 456 for (int j = 0; j < length; ++j) {
456 buffer[pos++] = random() % 10 + '0'; 457 buffer[pos++] = rand() % 10 + '0';
457 } 458 }
458 int exponent = DeterministicRandom() % (25*2 + 1) - 25 - length; 459 int exponent = DeterministicRandom() % (25*2 + 1) - 25 - length;
459 buffer[pos] = '\0'; 460 buffer[pos] = '\0';
460 Vector<const char> vector(buffer, pos); 461 Vector<const char> vector(buffer, pos);
461 double strtod_result = Strtod(vector, exponent); 462 double strtod_result = Strtod(vector, exponent);
462 CHECK(CheckDouble(vector, exponent, strtod_result)); 463 CHECK(CheckDouble(vector, exponent, strtod_result));
463 } 464 }
464 } 465 }
465 for (int length = 15; length < 800; length += 2) { 466 for (int length = 15; length < 800; length += 2) {
466 for (int i = 0; i < kLargeStrtodRandomCount; ++i) { 467 for (int i = 0; i < kLargeStrtodRandomCount; ++i) {
467 int pos = 0; 468 int pos = 0;
468 for (int j = 0; j < length; ++j) { 469 for (int j = 0; j < length; ++j) {
469 buffer[pos++] = random() % 10 + '0'; 470 buffer[pos++] = rand() % 10 + '0';
470 } 471 }
471 int exponent = DeterministicRandom() % (308*2 + 1) - 308 - length; 472 int exponent = DeterministicRandom() % (308*2 + 1) - 308 - length;
472 buffer[pos] = '\0'; 473 buffer[pos] = '\0';
473 Vector<const char> vector(buffer, pos); 474 Vector<const char> vector(buffer, pos);
474 double strtod_result = Strtod(vector, exponent); 475 double strtod_result = Strtod(vector, exponent);
475 CHECK(CheckDouble(vector, exponent, strtod_result)); 476 CHECK(CheckDouble(vector, exponent, strtod_result));
476 } 477 }
477 } 478 }
478 } 479 }
OLDNEW
« no previous file with comments | « test/cctest/test-strings.cc ('k') | test/cctest/test-utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698