|
1 | | -if(typeof require !== 'undefined'){ |
2 | | - var buster = require("buster"); |
3 | | - var SimplexNoise = require("../simplex-noise"); |
4 | | -} |
| 1 | +var SimplexNoise = require('../simplex-noise'); |
5 | 2 |
|
6 | | -var assert = buster.assert, |
7 | | - refute = buster.refute; |
| 3 | +var assert = require('chai').assert; |
8 | 4 |
|
9 | | -var _rnd; |
10 | | -function random(){ |
11 | | - return 1.0/(_rnd++); |
12 | | -} |
| 5 | +describe('simplex-noise', function() { |
| 6 | + function getRandom() { |
| 7 | + var rnd = 0; |
| 8 | + return function() { |
| 9 | + return 1.0 / (rnd++); |
| 10 | + }; |
| 11 | + } |
13 | 12 |
|
14 | | -buster.testCase("simplex-noise", { |
15 | | - setUp: function() {_rnd = 0;}, |
16 | | - "should initialize with Math.random": function () { |
17 | | - var simplex = new SimplexNoise(); |
18 | | - assert.equals(simplex.perm.length, 512); |
19 | | - assert.equals(simplex.permMod12.length, 512); |
20 | | - for(var i = 0; i < 512; i++){ |
21 | | - assert(simplex.perm[i] < 256); |
22 | | - assert(simplex.perm[i] >= 0); |
23 | | - assert(simplex.perm[i] >= 0); |
24 | | - assert.equals(simplex.perm[i], simplex.perm[i&255]); |
25 | | - assert.equals(simplex.permMod12[i], simplex.perm[i]%12); |
26 | | - } |
27 | | - }, |
28 | | - "should initialize with a custom random function": function () { |
29 | | - var i = 2, |
30 | | - simplex = new SimplexNoise(function(){return 1.0/i++;}); |
31 | | - assert.equals(simplex.perm.length, 512); |
32 | | - assert.equals(simplex.permMod12.length, 512); |
33 | | - assert.equals(simplex.perm[0], 128); |
34 | | - assert.equals(simplex.perm[1], 85); |
35 | | - assert.equals(simplex.perm[256], 128); |
36 | | - assert.equals(simplex.perm[257], 85); |
37 | | - assert.equals(simplex.permMod12[0], 128%12); |
38 | | - assert.equals(simplex.permMod12[1], 85%12); |
39 | | - }, |
40 | | - 'noise': { |
41 | | - setUp: function() { |
42 | | - this.simplex = new SimplexNoise(random); |
43 | | - }, |
44 | | - 'noise2D': { |
45 | | - 'should return the same value for the same input': function() { |
46 | | - assert.equals(this.simplex.noise2D(0.1, 0.2), this.simplex.noise2D(0.1, 0.2)); |
47 | | - }, |
48 | | - 'should return a different value for a different input': function() { |
49 | | - refute.equals(this.simplex.noise2D(0.1, 0.2), this.simplex.noise2D(0.101, 0.202)); |
50 | | - }, |
51 | | - 'should return values between -1 and 1': function () { |
52 | | - for(var x = 0; x < 10; x++) { |
53 | | - for(var y = 0; y < 10; y++) { |
54 | | - assert(this.simplex.noise2D(x/5, y/5) >= -1); |
55 | | - assert(this.simplex.noise2D(x/5, y/5) <= 1); |
56 | | - } |
57 | | - } |
58 | | - }, |
59 | | - 'should return similar values for similar inputs': function () { |
60 | | - assert(Math.abs(this.simplex.noise2D(0.1, 0.2)-this.simplex.noise2D(0.101, 0.202))<0.1); |
61 | | - } |
62 | | - }, |
63 | | - 'noise3D': { |
64 | | - 'should return the same value for the same input': function() { |
65 | | - assert.equals(this.simplex.noise3D(0.1, 0.2, 0.3), this.simplex.noise3D(0.1, 0.2, 0.3)); |
66 | | - }, |
67 | | - 'should return a different value for a different input': function() { |
68 | | - refute.equals(this.simplex.noise3D(0.1, 0.2, 0.3), this.simplex.noise3D(0.101, 0.202, 0.303)); |
69 | | - refute.equals(this.simplex.noise3D(0.1, 0.2, 0.3), this.simplex.noise3D(0.1, 0.2, 0.303)); |
70 | | - }, |
71 | | - 'should return values between -1 and 1': function () { |
72 | | - for(var x = 0; x < 10; x++) { |
73 | | - for(var y = 0; y < 10; y++) { |
74 | | - assert(this.simplex.noise3D(x/5, y/5, x+y) >= -1); |
75 | | - assert(this.simplex.noise3D(x/5, y/5, x+y) <= 1); |
76 | | - } |
77 | | - } |
78 | | - }, |
79 | | - 'should return similar values for similar inputs': function () { |
80 | | - assert(Math.abs(this.simplex.noise3D(0.1, 0.2, 0.3)-this.simplex.noise3D(0.101, 0.202, 0.303))<0.1); |
81 | | - } |
82 | | - }, |
83 | | - 'noise4D': { |
84 | | - 'should return the same value for the same input': function() { |
85 | | - assert.equals(this.simplex.noise4D(0.1, 0.2, 0.3, 0.4), this.simplex.noise4D(0.1, 0.2, 0.3, 0.4)); |
86 | | - }, |
87 | | - 'should return a different value for a different input': function() { |
88 | | - refute.equals(this.simplex.noise4D(0.1, 0.2, 0.3, 0.4), this.simplex.noise4D(0.101, 0.202, 0.303, 0.404)); |
89 | | - refute.equals(this.simplex.noise4D(0.1, 0.2, 0.3, 0.4), this.simplex.noise4D(0.1, 0.2, 0.3, 0.404)); |
90 | | - }, |
91 | | - 'should return values between -1 and 1': function () { |
92 | | - for(var x = 0; x < 10; x++) { |
93 | | - for(var y = 0; y < 10; y++) { |
94 | | - assert(this.simplex.noise4D(x/5, y/5, x+y, x-y) >= -1); |
95 | | - assert(this.simplex.noise4D(x/5, y/5, x+y, x-y) <= 1); |
96 | | - } |
97 | | - } |
98 | | - }, |
99 | | - 'should return similar values for similar inputs': function () { |
100 | | - assert(Math.abs(this.simplex.noise4D(0.1, 0.2, 0.3, 0.4)-this.simplex.noise4D(0.101, 0.202, 0.303, 0.404))<0.1); |
101 | | - } |
| 13 | + describe('constructor', function() { |
| 14 | + it('should initialize with Math.random', function() { |
| 15 | + var simplex = new SimplexNoise(); |
| 16 | + assert.equal(simplex.perm.length, 512); |
| 17 | + assert.equal(simplex.permMod12.length, 512); |
| 18 | + for (var i = 0; i < 512; i++) { |
| 19 | + assert(simplex.perm[i] < 256); |
| 20 | + assert(simplex.perm[i] >= 0); |
| 21 | + assert(simplex.perm[i] >= 0); |
| 22 | + assert.equal(simplex.perm[i], simplex.perm[i & 255]); |
| 23 | + assert.equal(simplex.permMod12[i], simplex.perm[i] % 12); |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + it('should initialize with a custom random function', function() { |
| 28 | + var i = 2; |
| 29 | + var simplex = new SimplexNoise(function() { |
| 30 | + return 1.0 / i++; |
| 31 | + }); |
| 32 | + assert.equal(simplex.perm.length, 512); |
| 33 | + assert.equal(simplex.permMod12.length, 512); |
| 34 | + assert.equal(simplex.perm[0], 128); |
| 35 | + assert.equal(simplex.perm[1], 85); |
| 36 | + assert.equal(simplex.perm[256], 128); |
| 37 | + assert.equal(simplex.perm[257], 85); |
| 38 | + assert.equal(simplex.permMod12[0], 128 % 12); |
| 39 | + assert.equal(simplex.permMod12[1], 85 % 12); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('noise', function() { |
| 44 | + var simplex; |
| 45 | + beforeEach(function() { |
| 46 | + simplex = new SimplexNoise(getRandom()); |
| 47 | + }); |
| 48 | + describe('noise2D', function() { |
| 49 | + it('should return the same value for the same input', function() { |
| 50 | + assert.equal(simplex.noise2D(0.1, 0.2), simplex.noise2D(0.1, 0.2)); |
| 51 | + }); |
| 52 | + it('should return a different value for a different input', function() { |
| 53 | + assert.notEqual(simplex.noise2D(0.1, 0.2), simplex.noise2D(0.101, 0.202)); |
| 54 | + }); |
| 55 | + it('should return values between -1 and 1', function() { |
| 56 | + for (var x = 0; x < 10; x++) { |
| 57 | + for (var y = 0; y < 10; y++) { |
| 58 | + assert(simplex.noise2D(x / 5, y / 5) >= -1); |
| 59 | + assert(simplex.noise2D(x / 5, y / 5) <= 1); |
| 60 | + } |
102 | 61 | } |
| 62 | + }); |
| 63 | + it('should return similar values for similar inputs', function() { |
| 64 | + assert(Math.abs(simplex.noise2D(0.1, 0.2) - simplex.noise2D(0.101, 0.202)) < 0.1); |
| 65 | + }); |
| 66 | + }); |
103 | 67 |
|
| 68 | + describe('noise3D', function() { |
| 69 | + it('should return the same value for the same input', function() { |
| 70 | + assert.equal(simplex.noise3D(0.1, 0.2, 0.3), simplex.noise3D(0.1, 0.2, 0.3)); |
| 71 | + }); |
| 72 | + it('should return a different value for a different input', function() { |
| 73 | + assert.notEqual(simplex.noise3D(0.1, 0.2, 0.3), simplex.noise3D(0.101, 0.202, 0.303)); |
| 74 | + assert.notEqual(simplex.noise3D(0.1, 0.2, 0.3), simplex.noise3D(0.1, 0.2, 0.303)); |
| 75 | + }); |
| 76 | + it('should return values between -1 and 1', function() { |
| 77 | + for (var x = 0; x < 10; x++) { |
| 78 | + for (var y = 0; y < 10; y++) { |
| 79 | + assert(simplex.noise3D(x / 5, y / 5, x + y) >= -1); |
| 80 | + assert(simplex.noise3D(x / 5, y / 5, x + y) <= 1); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + it('should return similar values for similar inputs', function() { |
| 85 | + assert(Math.abs(simplex.noise3D(0.1, 0.2, 0.3) - simplex.noise3D(0.101, 0.202, 0.303)) < 0.1); |
| 86 | + }); |
| 87 | + }); |
104 | 88 |
|
105 | | - } |
| 89 | + describe('noise4D', function() { |
| 90 | + it('should return the same value for the same input', function() { |
| 91 | + assert.equal(simplex.noise4D(0.1, 0.2, 0.3, 0.4), simplex.noise4D(0.1, 0.2, 0.3, 0.4)); |
| 92 | + }); |
| 93 | + it('should return a different value for a different input', function() { |
| 94 | + assert.notEqual(simplex.noise4D(0.1, 0.2, 0.3, 0.4), simplex.noise4D(0.101, 0.202, 0.303, 0.404)); |
| 95 | + assert.notEqual(simplex.noise4D(0.1, 0.2, 0.3, 0.4), simplex.noise4D(0.1, 0.2, 0.3, 0.404)); |
| 96 | + }); |
| 97 | + it('should return values between -1 and 1', function() { |
| 98 | + for (var x = 0; x < 10; x++) { |
| 99 | + for (var y = 0; y < 10; y++) { |
| 100 | + assert(simplex.noise4D(x / 5, y / 5, x + y, x - y) >= -1); |
| 101 | + assert(simplex.noise4D(x / 5, y / 5, x + y, x - y) <= 1); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + it('should return similar values for similar inputs', function() { |
| 106 | + assert( |
| 107 | + Math.abs( |
| 108 | + simplex.noise4D(0.1, 0.2, 0.3, 0.4) - |
| 109 | + simplex.noise4D(0.101, 0.202, 0.303, 0.404) |
| 110 | + ) < 0.1); |
| 111 | + }); |
| 112 | + }); |
| 113 | + }); |
106 | 114 | }); |
0 commit comments