From d890865c261c52f2f4c3f6747ea69254f52f34ab Mon Sep 17 00:00:00 2001 From: hzhang Date: Thu, 12 Dec 2024 12:33:00 +0000 Subject: [PATCH] add: test for custom field auto register --- tests/CustomFieldStructureTest.cs | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/CustomFieldStructureTest.cs diff --git a/tests/CustomFieldStructureTest.cs b/tests/CustomFieldStructureTest.cs new file mode 100644 index 0000000..e051d59 --- /dev/null +++ b/tests/CustomFieldStructureTest.cs @@ -0,0 +1,51 @@ +using System; +using Skeleton.Algebra.FieldStructures; + +namespace Skeleton.Test; + +public class CustomFieldStructureTest +{ + [CustomFieldStructure] + public class CustomField : FieldStructure + { + public override int Addition(int self, int other) => (self + other) % 7; + public override int AdditionUnit => 0; + public override int Multiplication(int self, int other) => (self * other) % 7; + + public override int MultiplicationUnit => 1; + public override int AdditionInverse(int self) => (7 - self) % 7; + + public override int MultiplicationInverse(int self) + { + for(int i = 0; i <= 7; i++ ) + if ((i * self) % 7 == 1) + return i; + throw new DivideByZeroException(); + } + + public override int Conj(int self) => self; + + public override bool IsEqualApprox(int self, int other, double? absTol = null, double? relTol = null) => + (self % 7) == (other % 7); + + public override string RawCSharpRepresentation(int x) => $"{x}"; + + public override double MaxError(int a) => (Math.Abs(a) % 7); + + public override int SquareRoot(int a) + { + for (int i = 0; i <= 7; i++) + { + if ((i * i) % 7 == a) + return i; + } + throw new ArgumentException("negative number in sqrt"); + } + } + + [Test] + public static void CustomerFieldStructureShouldNotBeNullTest() + { + Assert.IsNotNull(FieldStructure.Dispatch()); + } +} \ No newline at end of file