2026-04-13 15:13:13 +00:00
|
|
|
import 'dart:math' as Math;
|
|
|
|
|
|
2026-03-04 21:24:37 +00:00
|
|
|
import 'package:d4rt_formulas/formula_evaluator.dart';
|
|
|
|
|
import 'package:d4rt_formulas/formula_models.dart';
|
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
|
2026-03-05 17:27:07 +00:00
|
|
|
group("Formulas", (){
|
2026-03-05 11:47:10 +00:00
|
|
|
|
2026-03-05 17:27:07 +00:00
|
|
|
test("Solve x^2 formula", () {
|
|
|
|
|
final formula = Formula(
|
|
|
|
|
name: 'Test x^2',
|
|
|
|
|
input: [
|
|
|
|
|
VariableSpec(name: 'x', unit: 'scalar'),
|
|
|
|
|
],
|
|
|
|
|
output: VariableSpec(name: 'y', unit: 'scalar'),
|
|
|
|
|
d4rtCode: 'y = x*x;',
|
|
|
|
|
);
|
2026-03-05 11:47:10 +00:00
|
|
|
|
2026-03-05 17:27:07 +00:00
|
|
|
var solution = formulaSolver(formula, "x", {"y": 25}, maxDelta: 1e-10);
|
|
|
|
|
expect( solution, closeTo(5, 1e-10));
|
|
|
|
|
});
|
2026-03-04 21:24:37 +00:00
|
|
|
|
2026-04-13 15:13:13 +00:00
|
|
|
|
|
|
|
|
test("Solve x formula", () {
|
|
|
|
|
final formula = Formula(
|
|
|
|
|
name: 'Test x',
|
|
|
|
|
input: [
|
|
|
|
|
VariableSpec(name: 'x', unit: 'scalar'),
|
|
|
|
|
],
|
|
|
|
|
output: VariableSpec(name: 'y', unit: 'scalar'),
|
|
|
|
|
d4rtCode: 'y = x;',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var solution = formulaSolver(formula, "x", {"y": 123456789}, maxDelta: 1e-10);
|
|
|
|
|
expect(solution, closeTo(123456789, 1e-10));
|
|
|
|
|
});
|
2026-03-04 21:24:37 +00:00
|
|
|
});
|
|
|
|
|
|
2026-03-05 17:27:07 +00:00
|
|
|
group('Native functions', () {
|
|
|
|
|
test("Solve x^2", () {
|
|
|
|
|
Number f(Number x) => x * x;
|
|
|
|
|
var root = functionSolver(f, hint: 10, step: 1);
|
|
|
|
|
expect(root, closeTo(0, 0.1));
|
|
|
|
|
});
|
2026-03-04 21:24:37 +00:00
|
|
|
|
|
|
|
|
|
2026-03-05 17:27:07 +00:00
|
|
|
test("Solve (x-1000)^2", () {
|
|
|
|
|
Number f(Number x) => (x - 1000) * (x - 1000);
|
|
|
|
|
var root = functionSolver(f, hint: 10, step: 1, maxTries: 1000);
|
|
|
|
|
expect(root, closeTo(1000, 0.1));
|
|
|
|
|
});
|
2026-03-05 11:47:10 +00:00
|
|
|
|
2026-03-05 17:27:07 +00:00
|
|
|
test("Solve x^2 + 1", () {
|
|
|
|
|
Number f(Number x) => x * x + 1;
|
2026-03-05 11:47:10 +00:00
|
|
|
|
2026-03-05 17:27:07 +00:00
|
|
|
expect(() => functionSolver(f, hint: 10, step: 1),
|
|
|
|
|
throwsA(isA<NoSolutionException>()));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("Solve (x-2)(x-10", () {
|
|
|
|
|
Number f(Number x) => (x - 2) * (x - 10);
|
|
|
|
|
|
|
|
|
|
expect(functionSolver(f, hint: 10, step: 1), closeTo(10, 0.1));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Solve sqrt(x) = 2 => x = 4', () {
|
|
|
|
|
Number f(Number x) => Math.sqrt(x) - 2;
|
|
|
|
|
var root = functionSolver(f, hint: 5, step: 1);
|
|
|
|
|
expect(root, closeTo(4, 0.1));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Solve sin(x) = 0 near pi (hint 3)', () {
|
|
|
|
|
Number f(Number x) => Math.sin(x);
|
|
|
|
|
var root = functionSolver(f, hint: 3, step: 1);
|
|
|
|
|
expect(root, closeTo(Math.pi, 0.01));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Solve tan(x) = 1 => x = pi/4', () {
|
|
|
|
|
Number f(Number x) => Math.tan(x) - 1;
|
|
|
|
|
var root = functionSolver(f, hint: 0, step: 1);
|
|
|
|
|
expect(root, closeTo(Math.pi / 4, 0.01));
|
|
|
|
|
});
|
2026-03-05 11:47:10 +00:00
|
|
|
|
2026-03-05 17:27:07 +00:00
|
|
|
test('Solve exp(x) = 2 => x = ln(2)', () {
|
|
|
|
|
Number f(Number x) => Math.exp(x) - 2;
|
|
|
|
|
var root = functionSolver(f, hint: 1, step: 1);
|
|
|
|
|
expect(root, closeTo(Math.log(2), 0.01));
|
|
|
|
|
});
|
2026-03-05 11:47:10 +00:00
|
|
|
});
|
2026-03-04 21:24:37 +00:00
|
|
|
}
|