test: tests for maths methods

pull/14/head
RuiAlonso 4 years ago
parent 9ccf85c2ef
commit 8f6bad7a6f

@ -3,6 +3,7 @@ import 'package:flame/extensions.dart';
/// Method to calculate all points (with a required precision amount of them) /// Method to calculate all points (with a required precision amount of them)
/// of a circumference based on angle, offsetAngle and radius /// of a circumference based on angle, offsetAngle and radius
/// https://en.wikipedia.org/wiki/Trigonometric_functions
List<Vector2> calculateArc({ List<Vector2> calculateArc({
required Vector2 center, required Vector2 center,
required double radius, required double radius,
@ -47,9 +48,9 @@ List<Vector2> calculateBezierCurve({
final point = controlPoints[i]; final point = controlPoints[i];
xCoord += xCoord +=
_binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.x; binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.x;
yCoord += yCoord +=
_binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.y; binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.y;
} }
points.add(Vector2(xCoord, yCoord)); points.add(Vector2(xCoord, yCoord));
@ -61,21 +62,24 @@ List<Vector2> calculateBezierCurve({
/// Method to calculate the binomial coefficient of 'n' and 'k' /// Method to calculate the binomial coefficient of 'n' and 'k'
/// https://en.wikipedia.org/wiki/Binomial_coefficient /// https://en.wikipedia.org/wiki/Binomial_coefficient
num _binomial(num n, num k) { num binomial(num n, num k) {
assert(0 <= k && k <= n, 'Range 0<=k<=n'); assert(0 <= k && k <= n, 'Range 0<=k<=n');
if (k == 0 || n == k) { if (k == 0 || n == k) {
return 1; return 1;
} else { } else {
return _factorial(n) / (_factorial(k) * _factorial(n - k)); return factorial(n) / (factorial(k) * factorial(n - k));
} }
} }
/// Method to calculate the factorial of some number 'n' /// Method to calculate the factorial of some number 'n'
/// https://en.wikipedia.org/wiki/Factorial /// https://en.wikipedia.org/wiki/Factorial
num _factorial(num n) { num factorial(num n) {
if (n == 1) { assert(0 <= n, 'Non negative n');
if (n == 0) {
return 1;
} else if (n == 1) {
return 1; return 1;
} else { } else {
return n * _factorial(n - 1); return n * factorial(n - 1);
} }
} }

@ -6,10 +6,14 @@ publish_to: none
environment: environment:
sdk: ">=2.16.0 <3.0.0" sdk: ">=2.16.0 <3.0.0"
dependencies:
flame: ^1.0.0
flutter:
sdk: flutter
dev_dependencies: dev_dependencies:
coverage: ^1.1.0 flutter_test:
sdk: flutter
mocktail: ^0.2.0 mocktail: ^0.2.0
test: ^1.19.2 test: ^1.19.2
very_good_analysis: ^2.4.0 very_good_analysis: ^2.4.0
dependencies:
flame: ^1.0.0

@ -1,11 +1,90 @@
// ignore_for_file: prefer_const_constructors // ignore_for_file: prefer_const_constructors, cascade_invocations
import 'package:flutter_test/flutter_test.dart';
import 'package:maths/maths.dart'; import 'package:maths/maths.dart';
import 'package:test/test.dart';
class Binomial {
Binomial({required this.n, required this.k});
final num n;
final num k;
}
void main() { void main() {
group('Maths', () { group('Maths', () {
test('can be instantiated', () { group('calculateArc', () {});
expect(Maths(), isNotNull); group('calculateBezierCurve', () {});
group('binomial', () {
test('fails if k is negative', () {
expect(() => binomial(1, -1), throwsAssertionError);
});
test('fails if n is negative', () {
expect(() => binomial(-1, 1), throwsAssertionError);
});
test('fails if n < k', () {
expect(() => binomial(1, 2), throwsAssertionError);
});
test('for a specific input gives a correct value', () {
final binomialInputsToExpected = {
Binomial(n: 0, k: 0): 1,
Binomial(n: 1, k: 0): 1,
Binomial(n: 1, k: 1): 1,
Binomial(n: 2, k: 0): 1,
Binomial(n: 2, k: 1): 2,
Binomial(n: 2, k: 2): 1,
Binomial(n: 3, k: 0): 1,
Binomial(n: 3, k: 1): 3,
Binomial(n: 3, k: 2): 3,
Binomial(n: 3, k: 3): 1,
Binomial(n: 4, k: 0): 1,
Binomial(n: 4, k: 1): 4,
Binomial(n: 4, k: 2): 6,
Binomial(n: 4, k: 3): 4,
Binomial(n: 4, k: 4): 1,
Binomial(n: 5, k: 0): 1,
Binomial(n: 5, k: 1): 5,
Binomial(n: 5, k: 2): 10,
Binomial(n: 5, k: 3): 10,
Binomial(n: 5, k: 4): 5,
Binomial(n: 5, k: 5): 1,
Binomial(n: 6, k: 0): 1,
Binomial(n: 6, k: 1): 6,
Binomial(n: 6, k: 2): 15,
Binomial(n: 6, k: 3): 20,
Binomial(n: 6, k: 4): 15,
Binomial(n: 6, k: 5): 6,
Binomial(n: 6, k: 6): 1,
};
binomialInputsToExpected.forEach((input, value) {
expect(binomial(input.n, input.k), value);
});
});
});
group('factorial', () {
test('fails if negative number', () {
expect(() => factorial(-1), throwsAssertionError);
});
test('for a specific input gives a correct value', () {
final factorialInputsToExpected = {
0: 1,
1: 1,
2: 2,
3: 6,
4: 24,
5: 120,
6: 720,
7: 5040,
8: 40320,
9: 362880,
10: 3628800,
11: 39916800,
12: 479001600,
13: 6227020800,
};
factorialInputsToExpected.forEach((input, expected) {
expect(factorial(input), expected);
});
});
}); });
}); });
} }

Loading…
Cancel
Save