39 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Dart
		
	
	
		
		
			
		
	
	
			39 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Dart
		
	
	
| 
								 | 
							
								import 'package:flutter_test/flutter_test.dart';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import 'package:guru_utils/random/pseudo_random.dart'; // 导入PseudoRandom类的路径
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void main() {
							 | 
						||
| 
								 | 
							
								  group('PseudoRandom', () {
							 | 
						||
| 
								 | 
							
								    test('nextInt generates random integers', () {
							 | 
						||
| 
								 | 
							
								      final prng = PseudoRandom(42);
							 | 
						||
| 
								 | 
							
								      final numbers = List.generate(5, (_) => prng.nextInt());
							 | 
						||
| 
								 | 
							
								      expect(numbers, isNotEmpty);
							 | 
						||
| 
								 | 
							
								      expect(numbers, isNot(containsDuplicates(numbers)));
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    test('nextBool generates random booleans', () {
							 | 
						||
| 
								 | 
							
								      final prng = PseudoRandom(42);
							 | 
						||
| 
								 | 
							
								      final bools = List.generate(5, (_) => prng.nextBool());
							 | 
						||
| 
								 | 
							
								      expect(bools, isNotEmpty);
							 | 
						||
| 
								 | 
							
								      expect(bools, contains(true));
							 | 
						||
| 
								 | 
							
								      expect(bools, contains(false));
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    test('shuffle shuffles a list', () {
							 | 
						||
| 
								 | 
							
								      final prng = PseudoRandom(42);
							 | 
						||
| 
								 | 
							
								      final originalList = [1, 2, 3, 4, 5];
							 | 
						||
| 
								 | 
							
								      final shuffledList = List.from(originalList);
							 | 
						||
| 
								 | 
							
								      prng.shuffle(shuffledList);
							 | 
						||
| 
								 | 
							
								      expect(shuffledList, isNotEmpty);
							 | 
						||
| 
								 | 
							
								      expect(shuffledList, isNot(equals(originalList)));
							 | 
						||
| 
								 | 
							
								      expect(shuffledList.length, equals(originalList.length));
							 | 
						||
| 
								 | 
							
								      expect(shuffledList, containsAll(originalList));
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								  });
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// 辅助函数:检查给定的列表是否包含重复元素
							 | 
						||
| 
								 | 
							
								bool containsDuplicates(List list) {
							 | 
						||
| 
								 | 
							
								  return list.length != list.toSet().length;
							 | 
						||
| 
								 | 
							
								}
							 |