Skip to content

Commit

Permalink
unit tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym committed May 21, 2020
1 parent a00bbe4 commit 478cb17
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 45 deletions.
Expand Up @@ -8,7 +8,7 @@ import '../../../../dtype_to_title.dart';

void toJsonTestGroupFactory(DType dtype) =>
group(dtypeToMatrixTestTitle[dtype], () {
group('toJson', () {
group('to_json', () {
test('should return a serializable map', () {
final source = [
[1.0, 2.0, 3.0, 4.0],
Expand Down
@@ -0,0 +1,8 @@
import 'package:ml_linalg/dtype.dart';

import 'from_json_constructor_test_group_factory.dart';

void main() {
fromJsonConstructorTestGroupFactory(DType.float32);
fromJsonConstructorTestGroupFactory(DType.float64);
}
@@ -0,0 +1,36 @@
import 'package:ml_linalg/dtype.dart';
import 'package:ml_linalg/dtype_to_json.dart';
import 'package:ml_linalg/src/vector/vector_json_keys.dart';
import 'package:ml_linalg/vector.dart';
import 'package:ml_tech/unit_testing/matchers/iterable_almost_equal_to.dart';
import 'package:test/test.dart';

import '../../../../dtype_to_title.dart';

void fromJsonConstructorTestGroupFactory(DType dtype) =>
group(dtypeToVectorTestTitle[dtype], () {
group('fromJson constructor', () {
final data = <double>[12, 23, 44.0, -1e10, 10007.888, -10, 0, 7];
final json = {
vectorDTypeJsonKey: dTypeToJson(dtype),
vectorDataJsonKey: data,
};
final unknownJson = {
vectorDTypeJsonKey: 'some_unknown_dtype',
vectorDataJsonKey: data,
};

test('should decode serialized vector', () {
final vector = Vector.fromJson(json);

expect(vector, iterableAlmostEqualTo(data, 1e-3));
expect(vector.dtype, dtype);
});

test('should throw an error if unknown decoded value is passed', () {
final actual = () => Vector.fromJson(unknownJson);

expect(actual, throwsUnsupportedError);
});
});
});
@@ -0,0 +1,8 @@
import 'package:ml_linalg/dtype.dart';

import 'to_json_test_group_factory.dart';

void main() {
vectorToJsonTestGroupFactory(DType.float32);
vectorToJsonTestGroupFactory(DType.float64);
}
@@ -0,0 +1,24 @@
import 'package:ml_linalg/dtype.dart';
import 'package:ml_linalg/dtype_to_json.dart';
import 'package:ml_linalg/src/vector/vector_json_keys.dart';
import 'package:ml_linalg/vector.dart';
import 'package:ml_tech/unit_testing/matchers/iterable_almost_equal_to.dart';
import 'package:test/test.dart';

import '../../../../dtype_to_title.dart';

void vectorToJsonTestGroupFactory(DType dtype) =>
group(dtypeToVectorTestTitle[dtype], () {
group('toJson method', () {
final data = <double>[100, 129, 3.555, 5.07, -600, 17, 84];
final vector = Vector.fromList(data, dtype: dtype);

test('should return a json-serializable map', () {
final encoded = vector.toJson();

expect(encoded[vectorDTypeJsonKey], dTypeToJson(dtype));
expect(encoded[vectorDataJsonKey], iterableAlmostEqualTo(data));
expect(encoded.keys, hasLength(2));
});
});
});
44 changes: 0 additions & 44 deletions test/unit_test/vector/serialization/from_vector_json_test.dart

This file was deleted.

0 comments on commit 478cb17

Please sign in to comment.