From 187c926017542244450f52642af98c52e889b2e9 Mon Sep 17 00:00:00 2001 From: Rob Greene Date: Sun, 10 Jun 2018 10:38:08 -0500 Subject: [PATCH] Utility to generate all Applesoft variable names. --- .../api/utils/VariableNameGenerator.java | 28 +++++++++++++ .../api/utils/VariableNameGeneratorTest.java | 41 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 api/src/main/java/io/github/applecommander/bastokenizer/api/utils/VariableNameGenerator.java create mode 100644 api/src/test/java/io/github/applecommander/bastokenizer/api/utils/VariableNameGeneratorTest.java diff --git a/api/src/main/java/io/github/applecommander/bastokenizer/api/utils/VariableNameGenerator.java b/api/src/main/java/io/github/applecommander/bastokenizer/api/utils/VariableNameGenerator.java new file mode 100644 index 0000000..056c023 --- /dev/null +++ b/api/src/main/java/io/github/applecommander/bastokenizer/api/utils/VariableNameGenerator.java @@ -0,0 +1,28 @@ +package io.github.applecommander.bastokenizer.api.utils; + +import java.util.Optional; +import java.util.function.Supplier; + +/** Generate all Applesoft BASIC FP variable names. */ +public class VariableNameGenerator implements Supplier> { + public static final String CHAR1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + public static final String CHAR2 = " " + CHAR1 + "0123456789"; + public static final int LENGTH = CHAR1.length() * CHAR2.length(); + + private int n = 0; + + @Override + public Optional get() { + try { + if (n >= 0 && n < LENGTH) { + return Optional.of(String.format("%s%s", + CHAR1.charAt(n % CHAR1.length()), + CHAR2.charAt(n / CHAR1.length()) + ).trim()); + } + return Optional.empty(); + } finally { + n += 1; + } + } +} diff --git a/api/src/test/java/io/github/applecommander/bastokenizer/api/utils/VariableNameGeneratorTest.java b/api/src/test/java/io/github/applecommander/bastokenizer/api/utils/VariableNameGeneratorTest.java new file mode 100644 index 0000000..cd9269b --- /dev/null +++ b/api/src/test/java/io/github/applecommander/bastokenizer/api/utils/VariableNameGeneratorTest.java @@ -0,0 +1,41 @@ +package io.github.applecommander.bastokenizer.api.utils; + +import static org.junit.Assert.assertEquals; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +public class VariableNameGeneratorTest { + @Test + public void testNameSequence() { + Map expecteds = new HashMap<>(); + expecteds.put(0, "A"); + expecteds.put(25, "Z"); + expecteds.put(26, "AA"); + expecteds.put(51, "ZA"); + expecteds.put(52, "AB"); + expecteds.put(77, "ZB"); + // very last name in sequence + expecteds.put(VariableNameGenerator.LENGTH-1, "Z9"); + + int lastCheck = expecteds.keySet().stream().max(Integer::compare).get(); + + VariableNameGenerator gen = new VariableNameGenerator(); + for (int i = 0; i <= lastCheck; i++) { + String varName = gen.get().orElseThrow(() -> new RuntimeException("Ran out of variable names too early!")); + if (expecteds.containsKey(i)) { + assertEquals(expecteds.get(i), varName); + } + } + } + + @Test + public void testSequenceLength() { + VariableNameGenerator gen = new VariableNameGenerator(); + int count = 0; + while (gen.get().isPresent()) count++; + assertEquals(VariableNameGenerator.LENGTH, count); + } +}