FC18: Replace reminder with mask on the NCR1632 fraction encoder

This commit is contained in:
Willem Cazander 2025-08-07 17:36:20 +02:00
parent c6144b8956
commit c86e90fee7
4 changed files with 86 additions and 32 deletions

View file

@ -0,0 +1,65 @@
/*
* Copyright (c) 2004-2014, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.fc18;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* Tests FourCornerRecipe methods.
*
* @author Willem Cazander
* @version 1.0 Aug 7, 2025
*/
public class FourCornerRecipeTest {
@Test
public void testNCRCount1024() throws Exception {
List<Integer> cdc = new ArrayList<>();
for (int x = 1; x <= 1024; x++) {
BigInteger v = BigInteger.valueOf(x);
cdc.addAll(FourCornerRecipe.embedNCR1632Denominator(v));
//cdc.add(FourCornerDotCake.FC_NCR1632_XN.getStart()); // NXX_1
List<Integer> nomList = FourCornerRecipe.embedNCR1632Numerator(BigInteger.ONE);
Assertions.assertEquals(1, nomList.size());
cdc.addAll(nomList); // NXX_1
}
String res = FourCornerUnicodeDisplay.text().renderFromInt18(cdc);
Assertions.assertTrue(res.startsWith("¹/₁¹/₂"), "missing " + res);
Assertions.assertTrue(res.endsWith("¹/₁₀₂₃¹/₁₀₂₄"), "missing " + res);
}
@Test
public void testNCRMaxValue() throws Exception {
BigInteger maxValue = new BigInteger("FFFFFFFFFFFFFFFFFF"+"FFFFFFFFFFFFFFFFFF"+"FFFFFFFFFFFFFFFFFF"+"FFFFFFFFFFFFFFFFFF"+"FFFFFFFFFFFFFFFFFF"+"FFFFFFFFFFFFFFFFFF"+"FFFFFFFFFFFFFFFFFF"+"FFFFFFFFFFFFFFFFFF", 16);
FourCornerRecipe.embedNCR1632Numerator(maxValue);
Assertions.assertThrows(IllegalArgumentException.class, () -> {
FourCornerRecipe.embedNCR1632Numerator(maxValue.add(BigInteger.ONE));
});
}
}

View file

@ -133,21 +133,4 @@ public class LexerNCRTest {
String resSuber = "₂₄₇₃₃₀₄₀₁₄₇₃₁₀₄₅₃₄₀₆₀₅₀₂₅₂₁₀₁₉₆₄₇₁₉₀₀₃₅₁₃₁₃₄₉₁₀₁₂₁₁₈₃₉₉₁₄₀₆₃₀₅₆₀₉₂₈₉₇₂₂₅₁₀₆₅₃₁₈₆₇₁₇₀₃₁₆₄₀₁₀₆₁₂₄₃₀₄₄₉₈₉₅₉₇₆₇₁₄₂₆₀₁₆₁₃₉₃₃₉₃₅₁₃₆₅₀₃₄₃₀₆₇₅₁₂₀₉₉₆₇₅₄₆₁₅₅₁₀₁₈₉₃₁₆₇₉₁₆₆₀₆₇₇₂₁₄₈₆₉₉₁₃₆";
Assertions.assertEquals(resSuper + "/" + resSuber, FourCornerUnicodeDisplay.text().renderFromInt18(cdc));
}
@Test
public void testNCRCount1024() throws Exception {
List<Integer> cdc = new ArrayList<>();
for (int x = 1; x <= 1024; x++) {
BigInteger v = BigInteger.valueOf(x);
cdc.addAll(FourCornerRecipe.embedNCR1632Denominator(v));
//cdc.add(FourCornerDotCake.FC_NCR1632_XN.getStart()); // NXX_1
List<Integer> nomList = FourCornerRecipe.embedNCR1632Numerator(BigInteger.ONE);
Assertions.assertEquals(1, nomList.size());
cdc.addAll(nomList); // NXX_1
}
String res = FourCornerUnicodeDisplay.text().renderFromInt18(cdc);
Assertions.assertTrue(res.startsWith("¹/₁¹/₂"), "missing " + res);
Assertions.assertTrue(res.endsWith("¹/₁₀₂₃¹/₁₀₂₄"), "missing " + res);
}
}