Skip to content

Commit

Permalink
Revert RNG changes that didnt fix anything
Browse files Browse the repository at this point in the history
  • Loading branch information
daegalus committed Mar 29, 2024
1 parent 317ec2a commit 66cbac5
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 31 deletions.
15 changes: 1 addition & 14 deletions lib/rng.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,7 @@ abstract class RNG {
class MathRNG extends RNG {
final Random _rnd;

factory MathRNG.fromSeed({int? seed}) {
Random rnd = seed == null ? Random() : Random(seed);
return MathRNG._internal(rnd);
}

factory MathRNG.fromRandom({Random? rnd}) {
return MathRNG._internal(rnd ?? Random());
}

factory MathRNG.noSeed() {
return MathRNG._internal(Random());
}

const MathRNG._internal(this._rnd);
MathRNG({int? seed}) : _rnd = Random(seed);

@override
Uint8List generateInternal() {
Expand Down
3 changes: 1 addition & 2 deletions lib/v1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class UuidV1 {
/// Primarily sets up the seedBytes then generates the node id and clockseq
void _init() {
if (V1State.initialized) return;
Uint8List seedBytes =
goptions?.rng?.generate() ?? MathRNG.noSeed().generate();
Uint8List seedBytes = goptions?.rng?.generate() ?? MathRNG().generate();

// Per 4.5, create a 48-bit node id (47 random bits + multicast bit = 1)
List<int> nodeId = [
Expand Down
2 changes: 1 addition & 1 deletion lib/v4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class UuidV4 {
List<int> rng = options?.rng?.generate() ??
goptions?.rng?.generate() ??
options?.rng?.generate() ??
MathRNG.noSeed().generate();
MathRNG().generate();

// Use provided values over RNG
List<int> rnds = options?.random ?? rng;
Expand Down
3 changes: 1 addition & 2 deletions lib/v6.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class UuidV6 {
/// Primarily sets up the seedBytes then generates the node id and clockseq
void _init() {
if (V6State.initialized) return;
Uint8List seedBytes =
goptions?.rng?.generate() ?? MathRNG.noSeed().generate();
Uint8List seedBytes = goptions?.rng?.generate() ?? MathRNG().generate();

// Per 4.5, create a 48-bit node id (47 random bits + multicast bit = 1)
List<int> nodeId = [
Expand Down
2 changes: 1 addition & 1 deletion lib/v7.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class UuidV7 {

buf.setAll(0, timeList48);
List<int> randomBytes = options?.randomBytes ??
(goptions?.rng?.generate() ?? MathRNG.noSeed().generate());
(goptions?.rng?.generate() ?? MathRNG().generate());

buf.setRange(6, 16, randomBytes);
buf.setRange(6, 7, [buf.getRange(6, 7).last & 0x0f | 0x70]);
Expand Down
2 changes: 1 addition & 1 deletion lib/v8.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class UuidV8 {
5, 6, UuidParsing.parseHexToBytes(sprintf('0x%02i', [time.minute])));

var randomBytes = options?.randomBytes ??
(goptions?.rng?.generate() ?? MathRNG.noSeed().generate());
(goptions?.rng?.generate() ?? MathRNG().generate());

buf.setRange(6, 16, randomBytes);
buf.setRange(6, 7, [buf.getRange(6, 7).last & 0x0f | 0x80]);
Expand Down
20 changes: 10 additions & 10 deletions test/uuid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:uuid/uuid.dart';
import 'package:uuid/rng.dart';

void main() {
var uuid = Uuid();
var uuid = const Uuid();
const testTime = 1321644961388;

group('[Version 1 Tests]', () {
Expand Down Expand Up @@ -118,7 +118,7 @@ void main() {
group('[Version 4 Tests]', () {
test('Check if V4 is consistent using a static seed', () {
var u0 = uuid.v4(options: {
'rng': MathRNG.fromSeed(seed: 1),
'rng': MathRNG(seed: 1),
});
var u1 = 'a462502a-73af-4e41-bfc4-05957b7030dd';
expect(u0, equals(u1));
Expand All @@ -127,7 +127,7 @@ void main() {
test('Consistency check with buffer', () {
var buffer = Uint8List(16);
uuid.v4buffer(buffer, options: {
'rng': MathRNG.fromSeed(seed: 1),
'rng': MathRNG(seed: 1),
});

var u1 = 'a462502a-73af-4e41-bfc4-05957b7030dd';
Expand All @@ -136,10 +136,10 @@ void main() {

test('Using Objects', () {
var regular = uuid.v4(options: {
'rng': MathRNG.fromSeed(seed: 1),
'rng': MathRNG(seed: 1),
});
var obj = uuid.v4obj(options: {
'rng': MathRNG.fromSeed(seed: 1),
'rng': MathRNG(seed: 1),
});

expect(obj.uuid, equals(regular));
Expand Down Expand Up @@ -185,7 +185,7 @@ void main() {

var numDuplicates = 0;
for (var i = 0; i < numToGenerate; i++) {
final uuid = generator.v4(config: V4Options(null, MathRNG.noSeed()));
final uuid = generator.v4();

if (!values.contains(uuid)) {
values.add(uuid);
Expand Down Expand Up @@ -359,7 +359,7 @@ void main() {
});

test('Explicit options produce expected id', () {
final rand = MathRNG.fromSeed(seed: 1).generate();
final rand = MathRNG(seed: 1).generate();
var options = V7Options(1321651533573, rand);
var id = uuid.v7(config: options);

Expand Down Expand Up @@ -395,7 +395,7 @@ void main() {

test('Using buffers', () {
var buffer = Uint8List(16);
final rand = MathRNG.fromSeed(seed: 1).generate();
final rand = MathRNG(seed: 1).generate();
var options = V7Options(testTime, rand);

var wihoutBuffer = uuid.v7(config: options);
Expand All @@ -405,7 +405,7 @@ void main() {
});

test('Using Objects', () {
final rand = MathRNG.fromSeed(seed: 1).generate();
final rand = MathRNG(seed: 1).generate();
var options = V7Options(testTime, rand);

var regular = uuid.v7(config: options);
Expand Down Expand Up @@ -601,7 +601,7 @@ void main() {
],
}.entries) {
test(testCase.key, () {
final rand = MathRNG.fromSeed(seed: 1).generate();
final rand = MathRNG(seed: 1).generate();
final uuid =
Uuid().v8(config: V8Options(testCase.value[0] as DateTime, rand));
expect(uuid.toUpperCase(), equals(testCase.value[1]));
Expand Down

0 comments on commit 66cbac5

Please sign in to comment.