#!/bin/sh -e
# SPDX-FileCopyrightText: Chris Pressey, the original author of this work, has dedicated it to the public domain.
# For more information, please refer to <https://unlicense.org/>
# SPDX-License-Identifier: Unlicense
falderal doc/Definition-of-Fountain.md doc/Tests-for-fountain.md
# Tests for fuel consumption
expect() {
input=$(cat)
if [ "$input" = "$1" ]; then
return 0
else
echo "Expected '$1', got '$input'" >&2
return 1
fi
}
FOUNTAIN=./bin/fountain
### Fuel Consumption During Parsing ###
# Fuel factor 0 won't ever be enough to sustain anything.
echo -n "((*))" | $FOUNTAIN --dump-state --convert-loops --initial-fuel 0 --fuel-factor 0 parse eg/nested-parens.fountain -- n=2 | \
expect 'Left "Out of fuel"'
# But if you have enough fuel initially, it doesn't have to be sustainable.
echo -n "((*))" | $FOUNTAIN --dump-state --convert-loops --initial-fuel 4 --fuel-factor 0 parse eg/nested-parens.fountain -- n=2 | \
expect 'Right (Parsing {input = "", store = fromList [(Var "a",2),(Var "n",2)], fuel = FuelState {currentFuel = 1, fuelFactor = Just 0}})'
# Fuel factor 1 is sufficient for this parsing problem.
echo -n "((*))" | $FOUNTAIN --dump-state --convert-loops --initial-fuel 1 --fuel-factor 1 parse eg/nested-parens.fountain -- n=2 | \
expect 'Right (Parsing {input = "", store = fromList [(Var "a",2),(Var "n",2)], fuel = FuelState {currentFuel = 3, fuelFactor = Just 1}})'
# If you can do something with fuel factor 1, you can do it with fuel factor 2 as well.
echo -n "((*))" | $FOUNTAIN --dump-state --convert-loops --initial-fuel 1 --fuel-factor 2 parse eg/nested-parens.fountain -- n=2 | \
expect 'Right (Parsing {input = "", store = fromList [(Var "a",2),(Var "n",2)], fuel = FuelState {currentFuel = 8, fuelFactor = Just 2}})'
### Fuel Consumption During Generation ###
# Given enough fuel, it does generate.
$FOUNTAIN --convert-loops --fuel-factor 1 --initial-fuel 4 generate eg/nested-parens.fountain n=3 | \
expect '(((*)))'
# It does need some initial fuel.
$FOUNTAIN --convert-loops --fuel-factor 1 --initial-fuel 0 generate eg/nested-parens.fountain n=3 | \
expect 'Failure'
# Fuel gain, and usage, is proportional to the given n in this case.
$FOUNTAIN --convert-loops --fuel-factor 1 --initial-fuel 4 generate eg/nested-parens.fountain n=10 | \
expect '((((((((((*))))))))))'
# If fuel factor is 0 it is reliant on the initial fuel.
$FOUNTAIN --convert-loops --fuel-factor 0 --initial-fuel 4 generate eg/nested-parens.fountain n=10 | \
expect 'Failure'
$FOUNTAIN --convert-loops --fuel-factor 0 --initial-fuel 15 generate eg/nested-parens.fountain n=10 | \
expect '((((((((((*))))))))))'