Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
kev committed Jan 24, 2024
1 parent d4a8ea5 commit 0db26aa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 43 deletions.
1 change: 0 additions & 1 deletion CLVMDotNet/src/CLVM/Keywords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public static class Keywords
private static Dictionary<byte, string> InitializeKeywordFromAtom()
{
var keywordFromAtom = new Dictionary<byte, string>();
keywordFromAtom.Add(0x00, ".");
keywordFromAtom.Add(0x01, "q");
keywordFromAtom.Add(0x02, "a");
keywordFromAtom.Add(0x03, "i");
Expand Down
28 changes: 0 additions & 28 deletions CLVMDotNet/src/CLVM/Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,34 +220,6 @@ public static Tuple<BigInteger, SExp> ApplyOperator(byte[] atom, SExp args)

throw new Exception($"{BitConverter.ToString(atom).Replace("-", "")} Operator not found or is unsupported!");
}

public static string KEYWORD_FROM_ATOM(byte atom) => atom switch
{
0x23 => ".",
0x02 => "a",
0x01 => "q",
0x03 => "i",
0x04 => "c",
0x05 => "f",
0x06 => "r",
0x07 => "l",
0x08 => "x",
_ => throw new Exception("Invalid Atom")
};

public static byte KEYWORD_TO_ATOM(string keyword) => keyword switch
{
"." => 0x23,
"a" => 0x02,
"q" => 0x01,
"i" => 0x03,
"c" => 0x04,
"f" => 0x05,
"r" => 0x06,
"l" => 0x07,
"x" => 0x08,
_ => throw new Exception("Invalid Keyword")
};
}


Expand Down
36 changes: 30 additions & 6 deletions CLVMDotNet/tests/CLVM/Operators/KeywordFromAtomTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Xunit;
using x = CLVMDotNet.CLVM.Operator;
using x = CLVMDotNet.CLVM;

namespace CLVMDotNet.Tests.CLVM.Operators;

Expand All @@ -15,10 +15,34 @@ public class KeywordFromAtomTests
[InlineData(0x06, "r")]
[InlineData(0x07, "l")]
[InlineData(0x08, "x")]
[InlineData(0x0b, "sha256")]
[InlineData(0x0c, "substr")]
[InlineData(0x0d, "strlen")]
[InlineData(0x0e, "concat")]
[InlineData(0x0f, ".")]
[InlineData(0x10, "+")]
[InlineData(0x11, "-")]
[InlineData(0x12, "*")]
[InlineData(0x13, "/")]
[InlineData(0x14, "divmod")]
[InlineData(0x15, ">")]
[InlineData(0x16, "ash")]
[InlineData(0x17, "lsh")]
[InlineData(0x18, "logand")]
[InlineData(0x19, "logior")]
[InlineData(0x1a, "logxor")]
[InlineData(0x1b, "lognot")]
[InlineData(0x1c, ".")]
[InlineData(0x1d, "point_add")]
[InlineData(0x1e, "pubkey_for_exp")]
[InlineData(0x1f, ".")]
[InlineData(0x20, "not")]
[InlineData(0x21, "any")]
[InlineData(0x22, "all")]
public void KeywordToAtom_Returns_correct_byte(byte atom, string expectedKeyword)
{
var result = x.KEYWORD_FROM_ATOM(atom);
Assert.Equal(result, expectedKeyword);
var result = x.Keywords.KEYWORD_FROM_ATOM[atom];
Assert.Equal(expectedKeyword, result);
}

[Fact]
Expand All @@ -28,10 +52,10 @@ public void UnknownAtom_ThrowsError()

// Act
var errorMessage =
Assert.Throws<Exception>(() =>
x.KEYWORD_FROM_ATOM(0xaa));
Assert.Throws<KeyNotFoundException>(() =>
x.Keywords.KEYWORD_FROM_ATOM[0xaa]);

// Assert
Assert.Contains("Invalid Atom", errorMessage.Message);
Assert.Contains("The given key '170' was not present in the dictionary", errorMessage.Message);
}
}
39 changes: 31 additions & 8 deletions CLVMDotNet/tests/CLVM/Operators/KeywordToAtomTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Xunit;
using clvm = CLVMDotNet.CLVM.Operator;
using clvm = CLVMDotNet.CLVM;

namespace CLVMDotNet.Tests.CLVM.Operators;
namespace CLVMDotNet.Tests.CLVM;

public class KeywordToAtomTests
{
[Theory]
[InlineData(".", 0x23)]
[InlineData(".", 0x00)]
[InlineData("a", 0x02)]
[InlineData("q", 0x01)]
[InlineData("i", 0x03)]
Expand All @@ -15,10 +15,33 @@ public class KeywordToAtomTests
[InlineData("r", 0x06)]
[InlineData("l", 0x07)]
[InlineData("x", 0x08)]
[InlineData("=", 0x09)]
[InlineData("sha256", 0x0b)]
[InlineData("substr", 0x0c)]
[InlineData("strlen", 0x0d)]
[InlineData("concat", 0x0e)]
// [InlineData("#", 0x0f)]
[InlineData("+", 0x10)]
[InlineData("-", 0x11)]
[InlineData("*", 0x12)]
[InlineData("/", 0x13)]
[InlineData("divmod", 0x14)]
[InlineData(">", 0x15)]
[InlineData("ash", 0x16)]
[InlineData("lsh", 0x17)]
[InlineData("logand", 0x18)]
[InlineData("logior", 0x19)]
[InlineData("logxor", 0x1a)]
[InlineData("lognot", 0x1b)]
[InlineData("point_add", 0x1d)]
[InlineData("pubkey_for_exp", 0x1e)]
[InlineData("not", 0x20)]
[InlineData("any", 0x21)]
[InlineData("all", 0x22)]
public void KeywordToAtom_Returns_correct_byte(string keyword, byte expectedByte)
{
var result = clvm.KEYWORD_TO_ATOM(keyword);
Assert.Equal(result, expectedByte);
var result = clvm.Keywords.KEYWORD_TO_ATOM[keyword];
Assert.Equal(expectedByte, result);
}

[Fact]
Expand All @@ -28,11 +51,11 @@ public void UnknownKeyword_ThrowsError()

// Act
var errorMessage =
Assert.Throws<Exception>(() =>
clvm.KEYWORD_TO_ATOM("SomeInvalidKeyword"));
Assert.Throws<KeyNotFoundException>(() =>
clvm.Keywords.KEYWORD_TO_ATOM["SomeInvalidKeyword"]);

// Assert
Assert.Contains("Invalid Keyword", errorMessage.Message);
Assert.Contains("given key 'SomeInvalidKeyword' was not present in the dictionary", errorMessage.Message);
}
}

0 comments on commit 0db26aa

Please sign in to comment.