Skip to content

Instantly share code, notes, and snippets.

@nvie
Last active December 9, 2024 13:13
Show Gist options
  • Save nvie/3005a2d2ad6ee09f189298d14d966c64 to your computer and use it in GitHub Desktop.
Save nvie/3005a2d2ad6ee09f189298d14d966c64 to your computer and use it in GitHub Desktop.
import fc from "fast-check";
import isProbablyEnchantedWord from "./advent-day-09.mjs";
import { test, expect } from "bun:test";
const char = fc.stringMatching(/^\w$/);
const word = fc.stringMatching(/^\w+$/);
const palindrome = fc.oneof(
char,
fc
.tuple(word, fc.string({ maxLength: 1 }))
.map(([word, sep]) => word + sep + word.split("").reverse().join(""))
.filter((w) => !w.includes("\n"))
);
// declare isProbablyEnchantedWord(word: string): boolean;
test("day 9: false negatives", () => {
fc.assert(
fc.property(palindrome, (word) => {
expect(isProbablyEnchantedWord(word)).toEqual(true);
})
);
});
test("day 9: false positives", () => {
fc.assert(
fc.property(
word,
word,
(word1, word2) => {
fc.pre(word1.length >= 1);
fc.pre(!word1.includes(word2));
expect(isProbablyEnchantedWord(word2 + word1)).toEqual(false);
expect(isProbablyEnchantedWord(word1 + word2)).toEqual(false);
}
)
);
});
@nvie
Copy link
Author

nvie commented Dec 9, 2024

Counter example, a false positive:

ba

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment