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