mirror of
https://github.com/luxfi/safe-ios.git
synced 2026-10-30 15:19:20 +00:00
104 lines
4.4 KiB
Swift
104 lines
4.4 KiB
Swift
//
|
|
// File.swift
|
|
//
|
|
//
|
|
// Created by Dmitry Bespalov on 03.01.22.
|
|
//
|
|
|
|
import Foundation
|
|
import XCTest
|
|
import Solidity
|
|
|
|
class IntegerEncodingTests: XCTestCase {
|
|
|
|
func testEncodeOneWordNotAlignedToWord() throws {
|
|
let value = Sol.UInt8(0xef)
|
|
let data = value.encode()
|
|
let expected = Data([
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, // 32 bytes
|
|
])
|
|
XCTAssertEqual(data, expected)
|
|
}
|
|
|
|
func testEncodeTwoWordsNotAlignedToWord() {
|
|
let value = Sol.UInt72("ef00000000000000aa", radix: 16)!
|
|
let data = value.encode()
|
|
let expected: [UInt8] = [
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, // 32 bytes
|
|
]
|
|
XCTAssertEqual(Array(data), expected)
|
|
}
|
|
|
|
func testEncodeFourWords() {
|
|
let value = Sol.UInt256("ef000000000000000000000000000000000000000000000000000000000000aa", radix: 16)!
|
|
let data = value.encode()
|
|
let expected: [UInt8] = [
|
|
0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, // 32 bytes
|
|
]
|
|
XCTAssertEqual(Array(data), expected)
|
|
}
|
|
|
|
func testEncodeFourWordsMisaligned() {
|
|
let value = Sol.UInt248("ef0000000000000000000000000000000000000000000000000000000000aa", radix: 16)!
|
|
let data = value.encode()
|
|
let expected: [UInt8] = [
|
|
0x00, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, // 32 bytes
|
|
]
|
|
XCTAssertEqual(Array(data), expected)
|
|
}
|
|
|
|
func testDecodeUInt248() throws {
|
|
let data = Data([
|
|
0xaa, // extra byte to simulate start from the middle
|
|
0x00, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, // 32 bytes
|
|
])
|
|
let expected = Sol.UInt248("ef0000000000000000000000000000000000000000000000000000000000aa", radix: 16)!
|
|
var offset: Int = 1
|
|
let decoded = try Sol.UInt248(from: data, offset: &offset)
|
|
XCTAssertEqual(decoded, expected)
|
|
XCTAssertEqual(offset, 33)
|
|
}
|
|
|
|
func testDecodeUInt256() throws {
|
|
let data = Data([
|
|
0xaa, // extra byte to simulate start from the middle
|
|
0xef, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 16 bytes
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, // 32 bytes
|
|
])
|
|
let expected = Sol.UInt248("efef0000000000000000000000000000000000000000000000000000000000aa", radix: 16)!
|
|
var offset: Int = 1
|
|
let decoded = try Sol.UInt248(from: data, offset: &offset)
|
|
XCTAssertEqual(decoded, expected)
|
|
XCTAssertEqual(offset, 33)
|
|
}
|
|
|
|
func testEncodeInt8() {
|
|
let value = Sol.Int8(-3) // = 253 = 0xfd
|
|
let data = value.encode()
|
|
let expected = Data([
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 16 bytes
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, // 32 bytes
|
|
])
|
|
XCTAssertEqual(data, expected)
|
|
}
|
|
|
|
func testDecodeInt248() throws {
|
|
let data = Data([
|
|
0xaa, // extra byte to simulate start from the middle
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 16 bytes
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, // 32 bytes
|
|
])
|
|
let expected = Sol.Int248(-3)
|
|
var offset: Int = 1
|
|
let decoded = try Sol.Int248(from: data, offset: &offset)
|
|
XCTAssertEqual(decoded, expected)
|
|
XCTAssertEqual(offset, 33)
|
|
}
|
|
|
|
}
|