Tone.js/test/signal/Expr.js

141 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-08-24 21:29:19 +00:00
define(["Tone/signal/Signal", "Tone/signal/Expr", "Test", "helper/Basic",
2017-02-20 21:40:41 +00:00
"helper/OutputAudio", "helper/PassAudio", "helper/Offline", "helper/ConstantOutput"],
function(Signal, Expr, Test, Basic, OutputAudio, PassAudio, Offline, ConstantOutput){
2015-08-24 21:29:19 +00:00
describe("Expr", function(){
Basic(Expr, "1");
context("I/O", function(){
it("can create inputs", function(){
var exp = new Expr("$0 + $1");
Test.connect(exp, 0);
Test.connect(exp, 1);
exp.dispose();
});
it("has an output", function(){
var exp = new Expr("0 + 0");
exp.connect(Test);
exp.dispose();
});
2017-02-20 21:40:41 +00:00
it("outputs audio", function(){
return OutputAudio(function(){
new Expr("1.1").toMaster();
2015-08-24 21:29:19 +00:00
});
});
2017-02-20 21:40:41 +00:00
it("passes input", function(){
return PassAudio(function(input){
var exp = new Expr("$0");
2015-08-24 21:29:19 +00:00
input.connect(exp);
2017-02-20 21:40:41 +00:00
exp.toMaster();
2015-08-24 21:29:19 +00:00
});
});
});
context("Parsing", function(){
2017-02-20 21:40:41 +00:00
it("can do string replacements", function(){
return ConstantOutput(function(){
var exp = new Expr("% + %", 0.2, 0.8);
exp.toMaster();
}, 1);
2015-08-24 21:29:19 +00:00
});
2017-02-20 21:40:41 +00:00
it("can do string replacements with strings", function(){
return ConstantOutput(function(){
new Expr("%", "1 + 2").toMaster();
}, 3);
2015-08-24 21:29:19 +00:00
});
2017-02-20 21:40:41 +00:00
it("handles precendence", function(){
return ConstantOutput(function(){
new Expr("8 + 16 * 4 * (2 - 1)").toMaster();
}, 72);
2015-08-24 21:29:19 +00:00
});
2017-02-20 21:40:41 +00:00
it("tolerates inconsistent spacing", function(){
return ConstantOutput(function(){
new Expr("2 * 3-2 *4 ").toMaster();
}, -2);
2015-08-24 21:29:19 +00:00
});
2017-02-20 21:40:41 +00:00
it("handles parens", function(){
return ConstantOutput(function(){
new Expr("(8 + 16) * (4 - 1)").toMaster();
}, 72);
2015-08-24 21:29:19 +00:00
});
});
context("Math", function(){
2017-02-20 21:40:41 +00:00
it("does signal addition", function(){
return ConstantOutput(function(){
new Expr("1 + 3").toMaster();
}, 4);
2015-08-24 21:29:19 +00:00
});
2017-02-20 21:40:41 +00:00
it("does signal multiplication", function(){
return ConstantOutput(function(){
new Expr("1.5 * 6").toMaster();
}, 9);
2015-08-24 21:29:19 +00:00
});
2017-02-20 21:40:41 +00:00
it("does signal subtraction", function(){
return ConstantOutput(function(){
new Expr("8 - 16").toMaster();
}, -8);
2015-08-24 21:29:19 +00:00
});
});
context("Unary Operators", function(){
2017-02-20 21:40:41 +00:00
it("correctly outputs negative", function(){
return ConstantOutput(function(){
var sig = new Signal(1);
var exp = new Expr("-$0");
2015-08-24 21:29:19 +00:00
sig.connect(exp);
2017-02-20 21:40:41 +00:00
exp.toMaster();
}, -1);
2015-08-24 21:29:19 +00:00
});
});
context("Functions", function(){
2017-02-20 21:40:41 +00:00
it("handles abs(-1)", function(){
return ConstantOutput(function(){
new Expr("abs(-1)").toMaster();
}, 1);
2015-08-24 21:29:19 +00:00
});
2017-02-20 21:40:41 +00:00
it("handles abs(0.11)", function(){
return ConstantOutput(function(){
new Expr("abs(0.11)").toMaster();
}, 0.11);
2015-08-24 21:29:19 +00:00
});
2017-02-20 21:40:41 +00:00
it("handles mod(0.2, 0.9)", function(){
return ConstantOutput(function(){
new Expr("mod(0.2, 0.9)").toMaster();
}, 0.2);
2015-08-24 21:29:19 +00:00
});
2017-02-20 21:40:41 +00:00
it("handles mod(0.5, 0.25)", function(){
return ConstantOutput(function(){
new Expr("mod(0.6, 0.25)").toMaster();
}, 0.1);
2015-08-24 21:29:19 +00:00
});
2017-02-20 21:40:41 +00:00
it("computes pow(0.2, 3)", function(){
return ConstantOutput(function(){
new Expr("pow(0.2, 3)").toMaster();
}, 0.008, 0.001);
2015-08-24 21:29:19 +00:00
});
});
});
});