Add simple Grid - List Wrapper Data Structure
This commit is contained in:
parent
0c487f3427
commit
27ef72014e
2 changed files with 113 additions and 0 deletions
22
lib/src/Grid.dart
Normal file
22
lib/src/Grid.dart
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import 'dart:core';
|
||||||
|
|
||||||
|
import 'package:collection/collection.dart';
|
||||||
|
|
||||||
|
class Grid<E> extends DelegatingList<E> {
|
||||||
|
final List<E> _internal;
|
||||||
|
final width;
|
||||||
|
final height;
|
||||||
|
|
||||||
|
Grid(int width, int height) : this._(List<E>(width * height), width, height);
|
||||||
|
|
||||||
|
Grid.from(Grid<E> l)
|
||||||
|
: this._(List<E>.from(l.getRange(0, l.length)), l.width, l.height);
|
||||||
|
|
||||||
|
Grid.fromList(List<E> l, int width) : this._(l, width, l.length ~/ width);
|
||||||
|
|
||||||
|
Grid._(l, int w, int h)
|
||||||
|
: _internal = l,
|
||||||
|
width = w,
|
||||||
|
height = h,
|
||||||
|
super(l);
|
||||||
|
}
|
91
test/src/grid_test.dart
Normal file
91
test/src/grid_test.dart
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
import 'package:rules_of_living/src/Grid.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
@Tags(const ["nobrowser"])
|
||||||
|
void main() {
|
||||||
|
Grid sut;
|
||||||
|
group("Instantiation", () {
|
||||||
|
List<String> l;
|
||||||
|
setUp(() {
|
||||||
|
l = [
|
||||||
|
"Hey",
|
||||||
|
"you",
|
||||||
|
"me",
|
||||||
|
"together",
|
||||||
|
"Hello",
|
||||||
|
"World",
|
||||||
|
"I",
|
||||||
|
"am",
|
||||||
|
"ready."
|
||||||
|
];
|
||||||
|
});
|
||||||
|
test("gets created with the correct length for given quadratic gridsize",
|
||||||
|
() {
|
||||||
|
Grid sut = Grid(3, 3);
|
||||||
|
expect(sut.length, 9);
|
||||||
|
});
|
||||||
|
test("gets created with the correct length for given rectangular gridsize",
|
||||||
|
() {
|
||||||
|
Grid sut = Grid(87, 85);
|
||||||
|
expect(sut.length, 7395);
|
||||||
|
});
|
||||||
|
test("copies the content of another grid on .from Constructor call", () {
|
||||||
|
Grid original = Grid(2, 2);
|
||||||
|
original[0] = "Hey";
|
||||||
|
original[1] = "you";
|
||||||
|
original[2] = "me";
|
||||||
|
original[3] = "together";
|
||||||
|
|
||||||
|
Grid sut = Grid.from(original);
|
||||||
|
expect(sut, containsAllInOrder(["Hey", "you", "me", "together"]));
|
||||||
|
});
|
||||||
|
test("copies the length of another grid on .from Constructor call", () {
|
||||||
|
Grid original = Grid(2, 2);
|
||||||
|
original[0] = "Hey";
|
||||||
|
original[1] = "you";
|
||||||
|
original[2] = "me";
|
||||||
|
original[3] = "together";
|
||||||
|
|
||||||
|
Grid sut = Grid.from(original);
|
||||||
|
expect(sut.length, 4);
|
||||||
|
});
|
||||||
|
test("sets the length for list passed in on .fromList Constructor call",
|
||||||
|
() {
|
||||||
|
Grid sut = Grid.fromList(l, 3);
|
||||||
|
|
||||||
|
expect(sut.length, 9);
|
||||||
|
});
|
||||||
|
test("sets the contents of list passed in on .fromList Constructor call",
|
||||||
|
() {
|
||||||
|
Grid sut = Grid.fromList(l, 3);
|
||||||
|
|
||||||
|
expect(sut[3], "together");
|
||||||
|
});
|
||||||
|
test(
|
||||||
|
"sets the correct height for list passed in on .fromList Constructor call",
|
||||||
|
() {
|
||||||
|
Grid sut = Grid.fromList(l, 3);
|
||||||
|
|
||||||
|
expect(sut.width, 3);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
group("getter", () {
|
||||||
|
setUp(() {
|
||||||
|
sut = Grid(3, 3);
|
||||||
|
sut.setAll(0, [
|
||||||
|
"Hey",
|
||||||
|
"you",
|
||||||
|
"me",
|
||||||
|
"together",
|
||||||
|
"Hello",
|
||||||
|
"World",
|
||||||
|
"I",
|
||||||
|
"am",
|
||||||
|
"ready."
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
test("Engine can be instantiated without canvas", () {
|
||||||
|
expect(sut, isNot(throwsNoSuchMethodError));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in a new issue