Migrate Lumen to Dart and Flutter 2.0
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package com.lumen.daily;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CampaignTest {
|
||||
@Test public void everyPictureIsUniqueAndSolvableWithoutSearch() {
|
||||
Set<String> pictures = new HashSet<>();
|
||||
assertEquals(12, Campaign.LEVELS.length);
|
||||
for (int level = 0; level < Campaign.LEVELS.length; level++) {
|
||||
Puzzle p = Campaign.puzzle(level);
|
||||
assertEquals(7, p.size);
|
||||
assertEquals(49, p.solution.length);
|
||||
assertTrue(pictures.add(java.util.Arrays.toString(p.solution)));
|
||||
assertEquals("level " + level, 1, p.countSolutions());
|
||||
LogicSolver solver = new LogicSolver(49, p.constraints());
|
||||
LogicSolver.Analysis a = solver.analyze(LogicSolver.initial(p), true);
|
||||
assertTrue("level " + level, a.solved);
|
||||
assertArrayEquals(p.solution, a.cells);
|
||||
if (level >= 2) {
|
||||
assertEquals(3, p.regions.size());
|
||||
assertFalse("Area comparisons must actually be required at level " + level,
|
||||
solver.analyze(LogicSolver.initial(p), false).solved);
|
||||
assertTrue(a.areaDeductions > 0);
|
||||
// The added areas genuinely resolve ambiguity in the original rules.
|
||||
Puzzle rowsOnly = new Puzzle(7, p.solution);
|
||||
System.arraycopy(p.given, 0, rowsOnly.given, 0, p.given.length);
|
||||
assertEquals(2, rowsOnly.countSolutions());
|
||||
}
|
||||
for (Puzzle.Region r : p.regions) {
|
||||
assertTrue(r.top >= 0 && r.left >= 0 && r.top + r.height <= 7 && r.left + r.width <= 7);
|
||||
int sum = 0;
|
||||
for (int cell = 0; cell < 49; cell++) if (r.contains(cell, 7)) sum += p.solution[cell];
|
||||
assertEquals(sum, p.regionTotal(r));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void explainedHintsCanCompleteEveryStoryBoard() {
|
||||
for (int level = 0; level < 12; level++) {
|
||||
Puzzle p = Campaign.puzzle(level);
|
||||
LogicSolver solver = new LogicSolver(49, p.constraints());
|
||||
int[] known = LogicSolver.initial(p);
|
||||
for (int step = 0; step < 49; step++) {
|
||||
LogicSolver.Hint hint = solver.nextHint(known);
|
||||
if (hint == null) break;
|
||||
assertEquals(-1, known[hint.cell]);
|
||||
assertEquals(p.solution[hint.cell], hint.value);
|
||||
assertFalse(hint.explanation.isEmpty());
|
||||
known[hint.cell] = hint.value;
|
||||
}
|
||||
assertArrayEquals("level " + level, p.solution, known);
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void completionRequiresEveryMarkAndRegenerationIsStable() {
|
||||
for (int level = 0; level < 12; level++) {
|
||||
Puzzle p = Campaign.puzzle(level), q = Campaign.puzzle(level);
|
||||
assertArrayEquals(p.solution, q.solution);
|
||||
assertArrayEquals(p.given, q.given);
|
||||
int[] marks = new int[49];
|
||||
for (int i = 0; i < 49; i++) marks[i] = p.solution[i] == 1 ? 1 : 2;
|
||||
assertTrue(p.solved(marks));
|
||||
marks[48] = 0;
|
||||
assertFalse(p.solved(marks));
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void restoredProgressIsClamped() {
|
||||
assertEquals(0, Campaign.unlocked(-2));
|
||||
assertEquals(11, Campaign.unlocked(12));
|
||||
assertEquals(5, Campaign.unlocked(5));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.lumen.daily;
|
||||
import org.junit.Test;import static org.junit.Assert.*;
|
||||
public class DailyStreakTest {
|
||||
@Test public void consecutiveDaysAndReplay(){assertEquals(1,DailyStreak.afterCompletion(-99999,0,20000));assertEquals(4,DailyStreak.afterCompletion(20000,3,20001));assertEquals(3,DailyStreak.afterCompletion(20000,3,20000));}
|
||||
@Test public void missedDayResetsAndGraceLastsThroughNextDay(){assertEquals(1,DailyStreak.afterCompletion(20000,3,20002));assertEquals(3,DailyStreak.visible(20000,3,20001));assertEquals(0,DailyStreak.visible(20000,3,20002));}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.lumen.daily;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class DifficultyTest {
|
||||
@Test public void everyModeMatchesItsGradeWithoutGuessing() {
|
||||
for (Difficulty difficulty : Difficulty.values()) {
|
||||
for (long seed = 0; seed < 500; seed++) {
|
||||
Puzzle p = Puzzle.generate(seed, difficulty);
|
||||
assertEquals("seed " + seed, difficulty, p.difficulty());
|
||||
assertEquals(1, p.countSolutions());
|
||||
int rounds = p.deductionRounds();
|
||||
if (difficulty == Difficulty.EASY) assertTrue(rounds <= 2);
|
||||
if (difficulty == Difficulty.MEDIUM) assertTrue(rounds >= 3 && rounds <= 4);
|
||||
if (difficulty == Difficulty.HARD) assertEquals(5, rounds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void generationIsStableForSavedBoards() {
|
||||
for (Difficulty d : Difficulty.values()) for (int seed = 0; seed < 30; seed++) {
|
||||
Puzzle a = Puzzle.generate(seed, d), b = Puzzle.generate(seed, d);
|
||||
assertArrayEquals(a.solution, b.solution);
|
||||
assertArrayEquals(a.given, b.given);
|
||||
assertArrayEquals(a.rows, b.rows);
|
||||
assertArrayEquals(a.cols, b.cols);
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void graderUsesOnlyCluesAndRejectsAmbiguousBoards() {
|
||||
Puzzle p = new Puzzle(0);
|
||||
java.util.Arrays.fill(p.given, false);
|
||||
java.util.Arrays.fill(p.rows, 2);
|
||||
java.util.Arrays.fill(p.cols, 2);
|
||||
try { p.deductionRounds(); fail("Ambiguous board must not receive a grade"); }
|
||||
catch (IllegalStateException expected) { assertEquals("Puzzle requires guessing", expected.getMessage()); }
|
||||
}
|
||||
|
||||
@Test public void originalBoardsStayCompatible() {
|
||||
Puzzle p = new Puzzle(0);
|
||||
assertArrayEquals(new int[]{1,1,0,1,1,0,1,0,1,1,0,0,0,1,1,1,1,0,1,0,0,0,0,0,0},p.solution);
|
||||
}
|
||||
|
||||
@Test public void invalidStoredSelectionFallsBackToEasy() {
|
||||
assertEquals(Difficulty.EASY, Difficulty.fromStored("UNKNOWN"));
|
||||
assertEquals(Difficulty.HARD, Difficulty.fromStored("HARD"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.lumen.daily;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class LogicSolverTest {
|
||||
@Test public void overlappingDifferenceFindsWhatSingleTotalsCannot() {
|
||||
List<LogicSolver.Constraint> constraints = Arrays.asList(
|
||||
new LogicSolver.Constraint(new int[]{0, 1, 2}, 1, "Area A"),
|
||||
new LogicSolver.Constraint(new int[]{1, 2}, 1, "Area B"));
|
||||
LogicSolver solver = new LogicSolver(3, constraints);
|
||||
int[] initial = {-1, -1, -1};
|
||||
assertArrayEquals(initial, solver.analyze(initial, false).cells);
|
||||
LogicSolver.Hint hint = solver.nextHint(initial);
|
||||
assertNotNull(hint);
|
||||
assertEquals(0, hint.cell);
|
||||
assertEquals(0, hint.value);
|
||||
assertTrue(hint.explanation.contains("Shared"));
|
||||
assertEquals(0, solver.analyze(initial, true).cells[0]);
|
||||
}
|
||||
|
||||
@Test public void searchAndDeductionsAgreeWithExhaustiveEnumeration() {
|
||||
Random random = new Random(18273);
|
||||
for (int trial = 0; trial < 1000; trial++) {
|
||||
List<LogicSolver.Constraint> constraints = new ArrayList<>();
|
||||
for (int c = 0; c < 5; c++) {
|
||||
List<Integer> indices = new ArrayList<>();
|
||||
for (int i = 0; i < 6; i++) if (random.nextBoolean()) indices.add(i);
|
||||
int[] cells = new int[indices.size()];
|
||||
for (int i = 0; i < cells.length; i++) cells[i] = indices.get(i);
|
||||
constraints.add(new LogicSolver.Constraint(cells, random.nextInt(cells.length + 1)));
|
||||
}
|
||||
int[] initial = {-1, -1, -1, -1, -1, -1};
|
||||
if (random.nextBoolean()) initial[random.nextInt(6)] = random.nextInt(2);
|
||||
List<int[]> solutions = new ArrayList<>();
|
||||
for (int mask = 0; mask < 64; mask++) {
|
||||
int[] values = new int[6]; boolean valid = true;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
values[i] = (mask >> i) & 1;
|
||||
if (initial[i] >= 0 && initial[i] != values[i]) valid = false;
|
||||
}
|
||||
for (LogicSolver.Constraint c : constraints) {
|
||||
int sum = 0; for (int i : c.cells) sum += values[i];
|
||||
if (sum != c.total) valid = false;
|
||||
}
|
||||
if (valid) solutions.add(values);
|
||||
}
|
||||
LogicSolver solver = new LogicSolver(6, constraints);
|
||||
assertEquals(Math.min(2, solutions.size()), solver.countSolutions(initial));
|
||||
if (!solutions.isEmpty()) {
|
||||
LogicSolver.Hint hint = solver.nextHint(initial);
|
||||
LogicSolver.Analysis analysis = solver.analyze(initial, true);
|
||||
for (int[] solution : solutions) {
|
||||
if (hint != null) assertEquals(solution[hint.cell], hint.value);
|
||||
for (int i = 0; i < 6; i++) if (analysis.cells[i] >= 0) assertEquals(solution[i], analysis.cells[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.lumen.daily;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
public class PageTurnGestureTest {
|
||||
@Test public void gridDragCannotBecomeAPageTurn(){PageTurnGesture g=new PageTurnGesture();assertFalse(g.begin(300,400,100,700,22));assertFalse(g.intercept(650,400,28));assertEquals(0,g.finish(650,400,48));}
|
||||
@Test public void marginsTurnInBothDirections(){PageTurnGesture g=new PageTurnGesture();assertTrue(g.begin(690,400,100,700,22));assertTrue(g.intercept(600,401,28));assertEquals(1,g.finish(500,403,48));assertTrue(g.begin(110,400,100,700,22));assertEquals(-1,g.finish(300,410,48));}
|
||||
@Test public void verticalScrollTinyMovesAndCancellationDoNotTurn(){PageTurnGesture g=new PageTurnGesture();g.begin(110,400,100,700,22);assertEquals(0,g.finish(120,650,48));g.begin(110,400,100,700,22);assertEquals(0,g.finish(135,402,48));g.begin(110,400,100,700,22);g.cancel();assertEquals(0,g.finish(300,400,48));}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.lumen.daily;
|
||||
import org.junit.Test; import static org.junit.Assert.*;
|
||||
public class PuzzleTest {
|
||||
@Test public void generatedBoardsAreUniqueAndDeterministic(){for(long seed=0;seed<500;seed++){Puzzle p=new Puzzle(seed), q=new Puzzle(seed); assertEquals(1,p.countSolutions());assertArrayEquals(p.solution,q.solution);assertArrayEquals(p.given,q.given);int[] cells=new int[25];for(int i=0;i<25;i++)cells[i]=p.solution[i]==1?1:2;assertTrue(p.solved(cells));cells[0]=0;assertFalse(p.solved(cells));}}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.lumen.daily;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
public class StoryBookTest {
|
||||
@Test public void bookmarksResolveStableIdsAndUnknownIdsRecover(){
|
||||
String[] ids={"cottage.dedication","cottage.window","cottage.match.puzzle","cottage.match.memory","cottage.folded.note","cottage.lantern.sketch"};
|
||||
assertEquals(ids.length,StoryBook.PAGES.length);
|
||||
for(int i=0;i<ids.length;i++){assertEquals(ids[i],StoryBook.PAGES[i].id);assertEquals(i,StoryBook.indexOf(ids[i]));}
|
||||
assertEquals(0,StoryBook.indexOf("removed.page"));assertEquals(0,StoryBook.indexOf(null));
|
||||
}
|
||||
@Test public void pageBoundariesCannotEscapeTheBook(){assertEquals(0,StoryBook.turn(0,-1));assertEquals(5,StoryBook.turn(5,1));assertEquals(3,StoryBook.turn(2,1));}
|
||||
@Test public void spokenPassagesKeepSentenceBoundaries(){
|
||||
assertArrayEquals(new String[]{"My dear,","Come home.","Is the lamp lit?","Yes!"},StoryBook.sentences("My dear,\n\nCome home. Is the lamp lit? Yes!"));
|
||||
for(StoryBook.Page page:StoryBook.PAGES)for(String sentence:StoryBook.sentences(page.text))assertFalse(sentence.trim().isEmpty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user