Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 53 additions & 24 deletions B2R2.sln
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "B2R2.FrontEnd.Registers", "src\FrontEnd\Registers\B2R2.FrontEnd.Registers.fsproj", "{2E1CCF0A-F060-4752-9685-553A26EAAB61}"
EndProject
Expand Down Expand Up @@ -131,6 +132,10 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "B2R2.RearEnd.Utils", "src\R
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "B2R2.RearEnd.Visualization", "src\RearEnd\Visualization\B2R2.RearEnd.Visualization.fsproj", "{EDF9B76D-3EC8-4784-AECC-BC26C819F149}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "B2R2.RearEnd.BiHexLang", "src\RearEnd\BiHexLang\B2R2.RearEnd.BiHexLang.fsproj", "{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "B2R2.RearEnd.BiHexLang.Tests", "src\RearEnd\BiHexLang.Tests\B2R2.RearEnd.BiHexLang.Tests.fsproj", "{096BC321-EE34-4AB8-97FB-1028F6B40FE3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -501,6 +506,18 @@ Global
{11F98739-D053-45A0-BABF-EB564D272E49}.Release|x64.Build.0 = Release|Any CPU
{11F98739-D053-45A0-BABF-EB564D272E49}.Release|x86.ActiveCfg = Release|Any CPU
{11F98739-D053-45A0-BABF-EB564D272E49}.Release|x86.Build.0 = Release|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Debug|x64.ActiveCfg = Debug|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Debug|x64.Build.0 = Debug|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Debug|x86.ActiveCfg = Debug|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Debug|x86.Build.0 = Debug|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Release|Any CPU.Build.0 = Release|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Release|x64.ActiveCfg = Release|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Release|x64.Build.0 = Release|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Release|x86.ActiveCfg = Release|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Release|x86.Build.0 = Release|Any CPU
{6A9B21E8-F728-4C1F-9E9E-E78505B60922}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6A9B21E8-F728-4C1F-9E9E-E78505B60922}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A9B21E8-F728-4C1F-9E9E-E78505B60922}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -741,18 +758,6 @@ Global
{16BAADE1-0521-46FF-8B28-7ABB66CC17EB}.Release|x64.Build.0 = Release|Any CPU
{16BAADE1-0521-46FF-8B28-7ABB66CC17EB}.Release|x86.ActiveCfg = Release|Any CPU
{16BAADE1-0521-46FF-8B28-7ABB66CC17EB}.Release|x86.Build.0 = Release|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Debug|x64.ActiveCfg = Debug|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Debug|x64.Build.0 = Debug|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Debug|x86.ActiveCfg = Debug|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Debug|x86.Build.0 = Debug|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Release|Any CPU.Build.0 = Release|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Release|x64.ActiveCfg = Release|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Release|x64.Build.0 = Release|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Release|x86.ActiveCfg = Release|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Release|x86.Build.0 = Release|Any CPU
{008E997A-676E-4A23-90D3-F251B345150D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{008E997A-676E-4A23-90D3-F251B345150D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{008E997A-676E-4A23-90D3-F251B345150D}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -801,6 +806,18 @@ Global
{4A6D45B7-6692-4DE5-94EF-A1274EBF3315}.Release|x64.Build.0 = Release|Any CPU
{4A6D45B7-6692-4DE5-94EF-A1274EBF3315}.Release|x86.ActiveCfg = Release|Any CPU
{4A6D45B7-6692-4DE5-94EF-A1274EBF3315}.Release|x86.Build.0 = Release|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Debug|x64.ActiveCfg = Debug|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Debug|x64.Build.0 = Debug|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Debug|x86.ActiveCfg = Debug|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Debug|x86.Build.0 = Debug|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Release|Any CPU.Build.0 = Release|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Release|x64.ActiveCfg = Release|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Release|x64.Build.0 = Release|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Release|x86.ActiveCfg = Release|Any CPU
{8C0C08B4-B255-4FCE-B55C-B29C3A6A2060}.Release|x86.Build.0 = Release|Any CPU
{6F3E3BE4-04D6-466A-86A3-6C826F53C017}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F3E3BE4-04D6-466A-86A3-6C826F53C017}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F3E3BE4-04D6-466A-86A3-6C826F53C017}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -921,18 +938,30 @@ Global
{EDF9B76D-3EC8-4784-AECC-BC26C819F149}.Release|x64.Build.0 = Release|Any CPU
{EDF9B76D-3EC8-4784-AECC-BC26C819F149}.Release|x86.ActiveCfg = Release|Any CPU
{EDF9B76D-3EC8-4784-AECC-BC26C819F149}.Release|x86.Build.0 = Release|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Debug|x64.ActiveCfg = Debug|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Debug|x64.Build.0 = Debug|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Debug|x86.ActiveCfg = Debug|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Debug|x86.Build.0 = Debug|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Release|Any CPU.Build.0 = Release|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Release|x64.ActiveCfg = Release|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Release|x64.Build.0 = Release|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Release|x86.ActiveCfg = Release|Any CPU
{DF9FAFFE-0EBA-47AF-AD78-75E0D773099E}.Release|x86.Build.0 = Release|Any CPU
{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}.Debug|x64.ActiveCfg = Debug|Any CPU
{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}.Debug|x64.Build.0 = Debug|Any CPU
{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}.Debug|x86.ActiveCfg = Debug|Any CPU
{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}.Debug|x86.Build.0 = Debug|Any CPU
{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}.Release|Any CPU.Build.0 = Release|Any CPU
{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}.Release|x64.ActiveCfg = Release|Any CPU
{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}.Release|x64.Build.0 = Release|Any CPU
{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}.Release|x86.ActiveCfg = Release|Any CPU
{E8FCBA00-1A3F-4D5B-8167-5BD088172BF4}.Release|x86.Build.0 = Release|Any CPU
{096BC321-EE34-4AB8-97FB-1028F6B40FE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{096BC321-EE34-4AB8-97FB-1028F6B40FE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{096BC321-EE34-4AB8-97FB-1028F6B40FE3}.Debug|x64.ActiveCfg = Debug|Any CPU
{096BC321-EE34-4AB8-97FB-1028F6B40FE3}.Debug|x64.Build.0 = Debug|Any CPU
{096BC321-EE34-4AB8-97FB-1028F6B40FE3}.Debug|x86.ActiveCfg = Debug|Any CPU
{096BC321-EE34-4AB8-97FB-1028F6B40FE3}.Debug|x86.Build.0 = Debug|Any CPU
{096BC321-EE34-4AB8-97FB-1028F6B40FE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{096BC321-EE34-4AB8-97FB-1028F6B40FE3}.Release|Any CPU.Build.0 = Release|Any CPU
{096BC321-EE34-4AB8-97FB-1028F6B40FE3}.Release|x64.ActiveCfg = Release|Any CPU
{096BC321-EE34-4AB8-97FB-1028F6B40FE3}.Release|x64.Build.0 = Release|Any CPU
{096BC321-EE34-4AB8-97FB-1028F6B40FE3}.Release|x86.ActiveCfg = Release|Any CPU
{096BC321-EE34-4AB8-97FB-1028F6B40FE3}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
59 changes: 59 additions & 0 deletions src/RearEnd/BiHexLang.Tests/AST.Tests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(*
B2R2 - the Next-Generation Reversing Platform

Copyright (c) SoftSec Lab. @ KAIST, since 2016

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*)

namespace B2R2.RearEnd.BiHexLang.Tests

open Microsoft.VisualStudio.TestTools.UnitTesting
open B2R2.RearEnd.BiHexLang

[<TestClass>]
type ASTTests() =
let parser = Parser()

let (<=>) str (expected: string) =
match parser.Run str with
| Ok e ->
Assert.AreEqual<string>(expected, Expr.ToString e)
| Error e ->
System.Console.WriteLine $"{e}"
Assert.Fail("Parsing failed.")

[<TestMethod>]
member _.``Arithmetic op tests``() =
"1 + 2 - 3" <=> "((0x01 + 0x02) - 0x03)"
"1 + 2 * 3 / 4" <=> "(0x01 + ((0x02 * 0x03) / 0x04))"
"(1 + 2) * (3 - 4)" <=> "((0x01 + 0x02) * (0x03 - 0x04))"
"1 + 2 * (3 / 4)" <=> "(0x01 + (0x02 * (0x03 / 0x04)))"
"1 % 2 + 3" <=> "((0x01 % 0x02) + 0x03)"
"1 | (2 << 1)" <=> "(0x01 | (0x02 << 0x01))"
"(1 & 2) ^ 3" <=> "((0x01 & 0x02) ^ 0x03)"
"~0x4242 + 1" <=> "(~0x4242 + 0x01)"
"-1 * 0x42 - 0x42" <=> "((-0x01 * 0x42) - 0x42)"

[<TestMethod>]
member _.``Casting op tests``() =
"(dec) 0x42 + 1" <=> "((dec) 0x42 + 0x01)"
"(dec) 0x42 + (hex) 1" <=> "((dec) 0x42 + (hex) 0x01)"
"(oct) (0x42 * 2 + 0x42)" <=> "(oct) ((0x42 * 0x02) + 0x42)"
"(hex) ((oct) 0x42 + 1)" <=> "(hex) ((oct) 0x42 + 0x01)"
22 changes: 22 additions & 0 deletions src/RearEnd/BiHexLang.Tests/B2R2.RearEnd.BiHexLang.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Include="Number.Tests.fs" />
<Compile Include="AST.Tests.fs" />
<Compile Include="Evaluator.Tests.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="MSTest" Version="3.10.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BiHexLang\B2R2.RearEnd.BiHexLang.fsproj" />
</ItemGroup>

</Project>
84 changes: 84 additions & 0 deletions src/RearEnd/BiHexLang.Tests/Evaluator.Tests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
(*
B2R2 - the Next-Generation Reversing Platform

Copyright (c) SoftSec Lab. @ KAIST, since 2016

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*)

namespace B2R2.RearEnd.BiHexLang.Tests

open Microsoft.VisualStudio.TestTools.UnitTesting
open B2R2.RearEnd.BiHexLang

[<TestClass>]
type EvaluatorTests() =
let parser = Parser()
let evaluator = Evaluator()

let (==>) str (expected: string) =
match parser.Run str with
| Ok e ->
Assert.AreEqual<string>(expected, evaluator.EvalExprToString e)
| _ ->
Assert.Fail("Parsing failed.")

[<TestMethod>]
member _.``Arithmetic evaluation tests``() =
"1 + 2 - 3" ==> "0x00"
"0x1 * 2 - 0x4" ==> "0xFE"
"0d10 * 0d4 - 0d42" ==> "-0d2"
"0x00000000" ==> "0x00000000"
"(0x00004200 ^ 0x42420042 & 0x00FFFFFF)" ==> "0x00424242"
"0xFFFFFFFF + 0x1" ==> "0x00000000"
"0x00000000 - 0x1" ==> "0xFFFFFFFF"
"0xFFFF + 0x0002" ==> "0x0001"
"0x0000 - 0x0002" ==> "0xFFFE"

[<TestMethod>]
member _.``Arithmetic evaluation tests 2``() =
"0x10000000 + 0xFF" ==> "0x100000FF"
"0xFF0000FF - 0xFF" ==> "0xFF000000"
"0x00000002 * 0xFF" ==> "0x000001FE"
"0x00000008 / 0xFF" ==> "0x00000000"
"0x00000008 % 0xFF" ==> "0x00000008"
"0x10000008 & 0xFF" ==> "0x00000008"
"0x10000008 | 0xFF" ==> "0x100000FF"
"0x10000008 ^ 0xFF" ==> "0x100000F7"
"0xF0000008 << 0x4" ==> "0x00000080"
"0xFF << 0x4" ==> "0xF0"
"0xFF << 0x8" ==> "0x00"
"0xFF >> 0x4" ==> "0x0F"
"0xFF >> 0x8" ==> "0x00"

[<TestMethod>]
member _.``String tests``() =
"\"\"" ==> "0x00"
"\"ABCD\"" ==> "0x41424344"
"(dec) \"a\"" ==> "0d97"
"\"AA\" * 4" ==> "0x4141414141414141"
"0d10 * \"AB\"" ==> "0x4142414241424142414241424142414241424142"

[<TestMethod>]
member _.``Concatenation tests``() =
"(0x00004200 ^ 0x42420042 & 0x00FFFFFF) . 0xFFFF" ==> "0x00424242FFFF"
"0xFF0000 . (0xFFFF0000 | 0xFF) . 0x4242" ==> "0xFF0000FFFF00FF4242"
"0x82 . 0x8242 . 0x82424242" ==> "0x82824282424242"
"\"AAAAAAAA\" . 0x42424242" ==> "0x414141414141414142424242"
"(0x4 * 0x2) . 0x4242" ==> "0x084242"
102 changes: 102 additions & 0 deletions src/RearEnd/BiHexLang.Tests/Number.Tests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
(*
B2R2 - the Next-Generation Reversing Platform

Copyright (c) SoftSec Lab. @ KAIST, since 2016

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*)

namespace B2R2.RearEnd.BiHexLang.Tests

open Microsoft.VisualStudio.TestTools.UnitTesting
open B2R2.RearEnd.BiHexLang

[<TestClass>]
type NumberTests() =
let parser = Parser()

let testNum fmt bs result =
let expected = Number(fmt, bs)
match result with
| Ok(e: Expr) ->
Assert.AreEqual<Expr>(expected, e)
| Error e ->
System.Console.WriteLine $"{e}"
Assert.Fail("Parsing failed.")

let testToString (expected: string) str =
match parser.Run str with
| Ok e -> Assert.AreEqual<string>(expected, Expr.ToString e)
| _ -> Assert.Fail("Parsing failed.")

[<TestMethod>]
member _.``Hex number parsing tests``() =
testNum Hex [| 0x00uy; 0x00uy; 0x00uy; 0x00uy |] <| parser.Run "0x00000000"
testNum Hex [| 0x23uy; 0x01uy |] <| parser.Run "123"
testNum Hex [| 0x34uy; 0x12uy |] <| parser.Run "0x1234"
testNum Hex [| 0x01uy |] <| parser.Run "1"
testNum Hex [| 0x42uy |] <| parser.Run "42"
testNum Hex [| 0x42uy |] <| parser.Run "0x42"
testNum Hex [| 0x4uy; 0x3uy; 0x2uy; 0x1uy |] <| parser.Run "1020304"
testNum Hex [| 0x4uy; 0x3uy; 0x2uy; 0x1uy |] <| parser.Run "0x1020304"

[<TestMethod>]
member _.``Binary number parsing tests``() =
testNum Bin [| 0x5uy |] <| parser.Run "0b0101"
testNum Bin [| 0x5fuy |] <| parser.Run "0b01011111"
testNum Bin [| 0x44uy; 0x43uy; 0x42uy; 0x41uy |]
<| parser.Run "0b01000001010000100100001101000100"

[<TestMethod>]
member _.``Octal number parsing tests``() =
testNum Oct [| 0x1uy |] <| parser.Run "0o1"
testNum Oct [| 0xauy |] <| parser.Run "0o12"
testNum Oct [| 0x42uy |] <| parser.Run "0o102"
testNum Oct [| 0x6duy; 0x0buy |] <| parser.Run "0o5555"
testNum Oct [| 0x6duy; 0x5buy |] <| parser.Run "0o55555"
testNum Oct [| 0x6duy; 0xdbuy; 0x02uy |] <| parser.Run "0o555555"
testNum Oct [| 0x6duy; 0xdbuy; 0x16uy |] <| parser.Run "0o5555555"
testNum Oct [| 0x6duy; 0xdbuy; 0xb6uy |] <| parser.Run "0o55555555"
testNum Oct [| 0x6duy; 0xdbuy; 0xb6uy; 0x05uy |] <| parser.Run "0o555555555"

[<TestMethod>]
member _.``Decimal number parsing tests``() =
testNum Dec [| 0x0uy |] <| parser.Run "0d0"
testNum Dec [| 0x42uy |] <| parser.Run "0d66"
testNum Dec [| 0x78uy; 0x56uy; 0x34uy; 0x12uy |] <| parser.Run "0d305419896"

[<TestMethod>]
member _.``Number ToString tests``() =
testToString "0x1234" "0x1234"
testToString "0x1234" "1234"
testToString "0x00" "0x0"
testToString "0x01" "0x1"
testToString "0x12345678901234567890" "0x12345678901234567890"
testToString "0b0" "0b0"
testToString "0b1" "0b01"
testToString "0b101011010111101" "0b101011010111101"
testToString "0o0" "0o0"
testToString "0o4" "0o4"
testToString "0o123" "0o123"
testToString "0o12345" "0o12345"
testToString "0o1234512345" "0o1234512345"
testToString "0o123451234512345" "0o0000123451234512345"
testToString "0d0" "0d0"
testToString "0d42" "0d42"
testToString "0d175281381175281381" "0d175281381175281381"
Loading