---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by markus.
--- DateTime: 2/18/19 4:03 PM
---

--[[
    WireBait for wirebait is a lua package to help write Wireshark
    Dissectors in lua
    [Wirebait on Github](https://github.com/MarkoPaul0/WireBait)
    Copyright (C) 2015-2017 Markus Leballeux

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
]]

local is_standalone_test = not tester; --if only this file is being tested (not part of run all)
local tester = tester or require("tests.tester")

local Tvb       = require("wirebaitlib.packet_data.Tvb");
local TvbRange  = require("wirebaitlib.packet_data.TvbRange");
local ByteArray = require("wirebaitlib.primitives.ByteArray");

--[[ All variables here need to be kept local, however the unit test framework will run
each individual test function added with UnitTestsSet:addTest() in its own environment,
therefore forgetting the local keyword will not have a negative impact.
]]--
--Creating unit tests
local unit_tests = tester.newUnitTestsSet("TvbRange Unit Tests");

unit_tests:addTest("Testing TvbRange construction", function()
    local b = ByteArray.new("A0102FB1");
    local tvb_range = TvbRange.new(b);
    tester.assert(tvb_range.m_byte_array, b, "Wrong underlying data");
    tester.assert(tvb_range:len(), 4, "Wrong size after construction")
    tester.assert(tvb_range:offset(), 0, "Wrong offset after construction")
end);

unit_tests:addTest("Testing TvbRange:len()", function()
    tester.assert(TvbRange.new(ByteArray.new("4845")):len(), 2, "Wrong byte length");
end)

unit_tests:addTest("Testing TvbRange:offset()", function()
    tester.assert(TvbRange.new(ByteArray.new("4845")):offset(), 0, "Wrong offset");
    tester.assert(TvbRange.new(ByteArray.new("4845"), 1):offset(), 1, "Wrong offset");
    tester.assert(TvbRange.new(ByteArray.new("4845"), 2):offset(), 2, "Wrong offset");
    tester.assert(TvbRange.new(ByteArray.new("4845"), 7):offset(), 7, "Wrong offset");
end)

unit_tests:addTest("Testing TvbRange:tvb()", function()
    local byte_array = ByteArray.new("48450104FF081013AB0004C8");
    local tvb1 = TvbRange.new(byte_array):tvb();
    tester.assert(tvb1:bytes(), byte_array, "Wrong underlying data of Tvb returned by TvbRange:tvb()");
    tester.assert(tvb1:offset(), 0, "Wrong offset of Tvb returned by TvbRange:tvb()");
    local tvb2 = TvbRange.new(byte_array, 52):tvb();
    tester.assert(tvb2:bytes(), byte_array, "Wrong underlying data of Tvb returned by TvbRange:tvb()");
    tester.assert(tvb2:offset(), 52, "Wrong offset of Tvb returned by TvbRange:tvb()");
end)

unit_tests:addTest("Testing TvbRange:uint() (Big-Endian)", function()
    tester.assert(TvbRange.new(ByteArray.new("48454C4C")):uint(), 1212501068);
end)

unit_tests:addTest("Testing TvbRange:uint64() (Big-Endian)", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("6D45D89F55C23601")):uint64()), "7873937702377371137");
end)

unit_tests:addTest("Testing TvbRange:uint64() (Big-Endian) Largest uint64", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):uint64()), "18446744073709551615");
end)

unit_tests:addTest("Testing TvbRange:le_uint64() (Little-Endian) Largest uint64", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):le_uint64()), "18446744073709551615");
end)

unit_tests:addTest("Testing TvbRange:le_uint() (Little-Endian)", function()
    tester.assert(TvbRange.new(ByteArray.new("48454C4C")):le_uint(), 1280066888);
end)

unit_tests:addTest("Testing TvbRange:le_uint64() (Little-Endian)", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("48454C4C28520000")):le_uint64()), "90333032236360");
end)

unit_tests:addTest("Testing TvbRange:int() (Big-Endian) Negative Number", function()
    tester.assert(TvbRange.new(ByteArray.new("FFFF2852")):int(), -55214);
end)

unit_tests:addTest("Testing TvbRange:int() (Big-Endian) Zero", function()
    tester.assert(TvbRange.new(ByteArray.new("00000000")):int(), 0);
end)

unit_tests:addTest("Testing TvbRange:int() (Big-Endian) -1", function()
    tester.assert(TvbRange.new(ByteArray.new("FFFFFFFF")):int(), -1);
end)

unit_tests:addTest("Testing TvbRange:int() (Big-Endian) Largest Negative Number", function()
    tester.assert(TvbRange.new(ByteArray.new("80000000")):int(), -2147483648);
end)

unit_tests:addTest("Testing TvbRange:int() (Big-Endian) Largest Positive Number", function()
    tester.assert(TvbRange.new(ByteArray.new("7FFFFFFF")):int(), 2147483647);
end)

unit_tests:addTest("Testing TvbRange:int64() (Big-Endian) Zero", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("0000000000000000")):int64()), "0");
end)

unit_tests:addTest("Testing TvbRange:int64() (Big-Endian) -1", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):int64()), "-1");
end)

unit_tests:addTest("Testing TvbRange:int64() (Big-Endian) Large Negative Number", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("EFFFFFFFFFFFFFFF")):int64()), "-1152921504606846977");
end)

unit_tests:addTest("Testing TvbRange:int64() (Big-Endian) Largest Negative Number", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("8000000000000000")):int64()), "-9223372036854775808");
end)

unit_tests:addTest("Testing TvbRange:int64() (Big-Endian) Large Positive Number", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("7FFFFFFFFFFFFFFF")):int64()), "9223372036854775807");
end)

unit_tests:addTest("Testing TvbRange:le_int() (Little-Endian) Negative Number", function()
    tester.assert(TvbRange.new(ByteArray.new("5228FFFF")):le_int(), -55214);
end)

unit_tests:addTest("Testing TvbRange:le_int() (Little-Endian) Zero", function()
    tester.assert(TvbRange.new(ByteArray.new("00000000")):le_int(), 0);
end)

unit_tests:addTest("Testing TvbRange:le_int() (Little-Endian) -1", function()
    tester.assert(TvbRange.new(ByteArray.new("FFFFFFFF")):le_int(), -1);
end)

unit_tests:addTest("Testing TvbRange:le_int() (Little-Endian) Largest Negative Number", function()
    tester.assert(TvbRange.new(ByteArray.new("00000080")):le_int(), -2147483648);
end)

unit_tests:addTest("Testing TvbRange:le_int() (Little-Endian) Largest Positive Number", function()
    tester.assert(TvbRange.new(ByteArray.new("FFFFFF7F")):le_int(), 2147483647);
end)

unit_tests:addTest("Testing TvbRange:le_int64() (Little-Endian) Zero", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("0000000000000000")):le_int64()), "0");
end)

unit_tests:addTest("Testing TvbRange:le_int64() (Little-Endian) -1", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):le_int64()), "-1");
end)

unit_tests:addTest("Testing TvbRange:le_int64() (Little-Endian) Large Negative Number", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFEF")):le_int64()), "-1152921504606846977");
end)

unit_tests:addTest("Testing TvbRange:le_int64() (Little-Endian) Largest Negative Number", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("0000000000000080")):le_int64()), "-9223372036854775808");
end)

unit_tests:addTest("Testing TvbRange:le_int64() (Little-Endian) Large Positive Number", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFF7F")):le_int64()), "9223372036854775807");
end)

unit_tests:addTest("Testing TvbRange:float() (Big-Endian Single Precision) 1", function()
    tester.assert(TvbRange.new(ByteArray.new("3F800000")):float(), 1);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Single Precision) 1", function()
    tester.assert(TvbRange.new(ByteArray.new("0000803F")):le_float(), 1);
end)

unit_tests:addTest("Testing TvbRange:float() (Big-Endian Single Precision) -2", function()
    tester.assert(TvbRange.new(ByteArray.new("C0000000")):float(), -2);
end)

unit_tests:addTest("Testing TvbRange:float() (Big-Endian Single Precision) 0", function()
    tester.assert(TvbRange.new(ByteArray.new("00000000")):float(), 0);
end)

unit_tests:addTest("Testing TvbRange:float() (Big-Endian Single Precision) -0", function()
    tester.assert(TvbRange.new(ByteArray.new("80000000")):float(), 0);
end)

unit_tests:addTest("Testing TvbRange:float() (Big-Endian Single Precision) Infinity", function()
    tester.assert(TvbRange.new(ByteArray.new("7F800000")):float(), math.huge);
end)

unit_tests:addTest("Testing TvbRange:float() (Big-Endian Single Precision) -Infinity", function()
    tester.assert(TvbRange.new(ByteArray.new("FF800000")):float(), -math.huge);
end)

unit_tests:addTest("Testing TvbRange:float() (Big-Endian Single Precision) -0.15625", function()
    tester.assert(TvbRange.new(ByteArray.new("BE200000")):float(), -0.15625);
end)

unit_tests:addTest("Testing TvbRange:float() (Big-Endian Single Precision) 0.15625", function()
    tester.assert(TvbRange.new(ByteArray.new("3E200000")):float(), 0.15625);
end)

unit_tests:addTest("Testing TvbRange:float() (Big-Endian Double Precision) 1", function()
    tester.assert(TvbRange.new(ByteArray.new("3FF0000000000000")):float(), 1);
end)
unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Double Precision) 1", function()
    tester.assert(TvbRange.new(ByteArray.new("000000000000F03F")):le_float(), 1);
end)
unit_tests:addTest("Testing TvbRange:float() (Big-Endian Double Precision) -2", function()
    tester.assert(TvbRange.new(ByteArray.new("C000000000000000")):float(), -2);
end)
unit_tests:addTest("Testing TvbRange:float() (Big-Endian Double Precision) 0", function()
    tester.assert(TvbRange.new(ByteArray.new("0000000000000000")):float(), 0);
end)
unit_tests:addTest("Testing TvbRange:float() (Big-Endian Double Precision) -0", function()
    tester.assert(TvbRange.new(ByteArray.new("8000000000000000")):float(), 0);
end)
unit_tests:addTest("Testing TvbRange:float() (Big-Endian Double Precision) Infinity", function()
    tester.assert(TvbRange.new(ByteArray.new("7FF0000000000000")):float(), math.huge);
end)
unit_tests:addTest("Testing TvbRange:float() (Big-Endian Double Precision) -Infinity", function()
    tester.assert(TvbRange.new(ByteArray.new("FFF0000000000000")):float(), -math.huge);
end)

unit_tests:addTest("Testing TvbRange:float() (Big-Endian Double Precision) -Pi", function()
    tester.assert(TvbRange.new(ByteArray.new("C00921FB54442D18")):float(), -math.pi);
end)

unit_tests:addTest("Testing TvbRange:float() (Big-Endian Double Precision) Pi", function()
    tester.assert(TvbRange.new(ByteArray.new("400921FB54442D18")):float(), math.pi);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Single Precision) -2", function()
    tester.assert(TvbRange.new(ByteArray.new("000000C0")):le_float(), -2);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Single Precision) 0", function()
    tester.assert(TvbRange.new(ByteArray.new("00000000")):le_float(), 0);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Single Precision) -0", function()
    tester.assert(TvbRange.new(ByteArray.new("00000080")):le_float(), 0);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Single Precision) Infinity", function()
    tester.assert(TvbRange.new(ByteArray.new("0000807F")):le_float(), math.huge);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Single Precision) -Infinity", function()
    tester.assert(TvbRange.new(ByteArray.new("000080FF")):le_float(), -math.huge);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Single Precision) -0.15625", function()
    tester.assert(TvbRange.new(ByteArray.new("000020BE")):le_float(), -0.15625);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Single Precision) 0.15625", function()
    tester.assert(TvbRange.new(ByteArray.new("0000203E")):le_float(), 0.15625);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Double Precision) -2", function()
    tester.assert(TvbRange.new(ByteArray.new("00000000000000C0")):le_float(), -2);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Double Precision) 0", function()
    tester.assert(TvbRange.new(ByteArray.new("0000000000000000")):le_float(), 0);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Double Precision) -0", function()
    tester.assert(TvbRange.new(ByteArray.new("0000000000000080")):le_float(), 0);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Double Precision) Infinity", function()
    tester.assert(TvbRange.new(ByteArray.new("000000000000F07F")):le_float(), math.huge);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Double Precision) -Infinity", function()
    tester.assert(TvbRange.new(ByteArray.new("000000000000F0FF")):le_float(), -math.huge);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Double Precision) -Pi", function()
    tester.assert(TvbRange.new(ByteArray.new("182D4454FB2109C0")):le_float(), -math.pi);
end)

unit_tests:addTest("Testing TvbRange:le_float() (Little-Endian Double Precision) Pi", function()
    tester.assert(TvbRange.new(ByteArray.new("182D4454FB210940")):le_float(), math.pi);
end)

unit_tests:addTest("Testing TvbRange:bitfield(11,3) = 4", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("AB123FC350DDB12D")):bitfield(11,3)), "4");
end)

unit_tests:addTest("Testing TvbRange:bitfield(11,5) = 18", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("AB123FC350DDB12D")):bitfield(11,5)), "18");
end)

unit_tests:addTest("Testing TvbRange:bitfield(0,8) = 171", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("AB123FC350DDB12D")):bitfield(0,8)), "171");
end)

unit_tests:addTest("Testing TvbRange:bitfield(0,33) = 5740199814", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("AB123FC350DDB12D")):bitfield(0,33)), "5740199814");
end)

unit_tests:addTest("Testing TvbRange:bitfield(0,8) = 255", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):bitfield(0,8)), "255");
end)

unit_tests:addTest("Testing TvbRange:bitfield(0,16) = 65535", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):bitfield(0,16)), "65535");
end)

unit_tests:addTest("Testing TvbRange:bitfield(0,32) = 4294967295", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):bitfield(0,32)), "4294967295");
end)

unit_tests:addTest("Testing TvbRange:bitfield(0,33) = 8589934591", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):bitfield(0,33)), "8589934591");
end)

unit_tests:addTest("Testing TvbRange:bitfield(0,56) = 72057594037927935", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):bitfield(0,56)), "72057594037927935");
end)

unit_tests:addTest("Testing TvbRange:bitfield(0,57) = 144115188075855871", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):bitfield(0,57)), "144115188075855871");
end)

unit_tests:addTest("Testing TvbRange:bitfield(0,58) = 288230376151711743", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):bitfield(0,58)), "288230376151711743");
end)

unit_tests:addTest("Testing TvbRange:bitfield(0,63) = 9223372036854775807", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):bitfield(0,63)), "9223372036854775807");
end)

unit_tests:addTest("Testing TvbRange:bitfield(0,64) = -1", function()
    tester.assert(tostring(TvbRange.new(ByteArray.new("FFFFFFFFFFFFFFFF")):bitfield(0,64)), "18446744073709551615");
end)

unit_tests:addTest("Testing TvbRange:string()", function()
    tester.assert(TvbRange.new(ByteArray.new("48454C4C4F20574F524C44")):string(),"HELLO WORLD", "Wrong result.");
end)

unit_tests:addTest("Testing TvbRange:stringz()", function()
    tester.assert(TvbRange.new(ByteArray.new("48454C4C4F20574F524C440032b4b1b34b2b")):stringz(),"HELLO WORLD", "Wrong result.");
end)

unit_tests:addTest("Testing TvbRange:ipv4() (Big-Endian) 192.168.0.255", function()
    tester.assert(TvbRange.new(ByteArray.new("C0A800FF")):ipv4(), "192.168.0.255");
end)

unit_tests:addTest("Testing TvbRange:le_ipv4() (Little-Endian) 192.168.0.FF", function()
    tester.assert(TvbRange.new(ByteArray.new("FF00A8C0")):le_ipv4(), "192.168.0.255");
end)

unit_tests:addTest("Testing TvbRange:eth() (Big-Endian) ec:08:6b:70:36:82", function()
    tester.assert(TvbRange.new(ByteArray.new("EC086B703682")):eth(), "ec:08:6b:70:36:82");
end)

unit_tests:addTest("Testing TvbRange:guid() = 48454c4c-4f20-574f-524c-440032b4b1b3", function()
    tester.assert(TvbRange.new(ByteArray.new("48454C4C4F20574F524C440032b4b1b3")):guid(), "48454c4c-4f20-574f-524c-440032b4b1b3");
end)

if is_standalone_test then
    tester.test(unit_tests);
    tester.printReport();
else
    return unit_tests
end
