diff --git a/pubspec.yaml b/pubspec.yaml index d6bda45..2291deb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -6,3 +6,6 @@ homepage: https://github.com/pd4d10/nanoid environment: sdk: ">=2.0.0-dev.32.0 <2.0.0" + +dev_dependencies: + test: ^0.12.33 diff --git a/test/generate_test.dart b/test/generate_test.dart new file mode 100644 index 0000000..42afb6f --- /dev/null +++ b/test/generate_test.dart @@ -0,0 +1,29 @@ +import 'package:test/test.dart'; +import 'package:nanoid/generate.dart'; + +void main() { + test('has options', () { + expect(generate('a', 5), equals('aaaaa')); + }); + + test('has flat distribution', () { + var COUNT = 100 * 1000; + var LENGTH = 5; + var ALPHABET = 'abcdefghijklmnopqrstuvwxyz'; + + var chars = {}; + for (var i = 0; i < COUNT; i++) { + var id = generate(ALPHABET, LENGTH); + for (var j = 0; j < id.length; j++) { + var char = id[j]; + if (chars[char] == null) chars[char] = 0; + chars[char] += 1; + } + } + + chars.forEach((k, _) { + var distribution = (chars[k] * ALPHABET.length) / (COUNT * LENGTH); + expect(distribution, closeTo(1, 1)); + }); + }); +} diff --git a/test/nanoid_test.dart b/test/nanoid_test.dart new file mode 100644 index 0000000..481d67e --- /dev/null +++ b/test/nanoid_test.dart @@ -0,0 +1,25 @@ +import 'package:test/test.dart'; +import 'package:nanoid/nanoid.dart'; +import 'package:nanoid/url.dart'; + +void main() { + test('generates URL-friendly IDs', () { + for (var i = 0; i < 10; i++) { + var id = nanoid(); + expect(id.length, equals(21)); + for (var j = 0; j < id.length; j++) { + expect(url.contains(id[j]), equals(true)); + } + } + }); + + test('has no collisions', () { + var COUNT = 100 * 1000; + var used = {}; + for (var i = 0; i < COUNT; i++) { + var id = nanoid(); + expect(used[id], equals(null)); + used[id] = true; + } + }); +}