mirror of
https://github.com/luxfi/safe-ios.git
synced 2026-10-30 15:19:20 +00:00
114 lines
5.1 KiB
Swift
114 lines
5.1 KiB
Swift
//
|
|
// File.swift
|
|
//
|
|
//
|
|
// Created by Dmitry Bespalov on 03.01.22.
|
|
//
|
|
|
|
import Foundation
|
|
import Solidity
|
|
import XCTest
|
|
|
|
class FixedBytesEncodingTests: XCTestCase {
|
|
func testEncodeBytes1() {
|
|
let value = Sol.Bytes1(storage: Data([0x01]))
|
|
let data = value.encode()
|
|
let expected = Data([
|
|
0x01, 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, 0x00, // 32 bytes
|
|
])
|
|
XCTAssertEqual(data, expected)
|
|
}
|
|
|
|
func testDecodeBytes1() throws {
|
|
let expected = Sol.Bytes1(storage: Data([0x01]))
|
|
let data = Data([
|
|
0x01, 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, 0x00, // 32 bytes
|
|
])
|
|
var offset = 0
|
|
let value = try Sol.Bytes1(from: data, offset: &offset)
|
|
XCTAssertEqual(Array(value.storage), Array(expected.storage))
|
|
XCTAssertEqual(offset, 32)
|
|
}
|
|
|
|
func testEncodeBytes2() {
|
|
let value = Sol.Bytes2(storage: Data([0x01, 0x02]))
|
|
let data = value.encode()
|
|
let expected = Data([
|
|
0x01, 0x02, 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, 0x00, // 32 bytes
|
|
])
|
|
XCTAssertEqual(data, expected)
|
|
}
|
|
|
|
func testDecodeBytes2() throws {
|
|
let expected = Sol.Bytes2(storage: Data([0x01, 0x02]))
|
|
let data = Data([
|
|
0x01, 0x02, 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, 0x00, // 32 bytes
|
|
])
|
|
var offset = 0
|
|
let value = try Sol.Bytes2(from: data, offset: &offset)
|
|
XCTAssertEqual(Array(value.storage), Array(expected.storage))
|
|
XCTAssertEqual(offset, 32)
|
|
}
|
|
|
|
func testEncodeBytes31() {
|
|
let value = Sol.Bytes31(storage: Data([
|
|
0x01, 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, 0x31 // 31 byte
|
|
]))
|
|
let data = value.encode()
|
|
let expected = Data([
|
|
0x01, 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, 0x31, 0x00, // 32 bytes
|
|
])
|
|
XCTAssertEqual(data, expected)
|
|
}
|
|
|
|
func testDecodeBytes31() throws {
|
|
let expected = Sol.Bytes31(storage: Data([
|
|
0x01, 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, 0x31 // 31 byte
|
|
]))
|
|
let data = Data([
|
|
0x01, 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, 0x31, 0x00, // 32 bytes
|
|
])
|
|
var offset = 0
|
|
let value = try Sol.Bytes31(from: data, offset: &offset)
|
|
XCTAssertEqual(Array(value.storage), Array(expected.storage))
|
|
XCTAssertEqual(offset, 32)
|
|
}
|
|
|
|
func testEncodeBytes32() {
|
|
let value = Sol.Bytes32(storage: Data([
|
|
0x01, 0x02, 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, 0x32, // 32 bytes
|
|
]))
|
|
let data = value.encode()
|
|
let expected = Data([
|
|
0x01, 0x02, 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, 0x32, // 32 bytes
|
|
])
|
|
XCTAssertEqual(data, expected)
|
|
}
|
|
|
|
func testDecodeBytes32() throws {
|
|
let expected = Sol.Bytes32(storage: Data([
|
|
0x01, 0x02, 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, 0x32, // 32 bytes
|
|
]))
|
|
let data = Data([
|
|
0x01, 0x02, 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, 0x32, // 32 bytes
|
|
])
|
|
var offset = 0
|
|
let value = try Sol.Bytes32(from: data, offset: &offset)
|
|
XCTAssertEqual(Array(value.storage), Array(expected.storage))
|
|
XCTAssertEqual(offset, 32)
|
|
}
|
|
|
|
}
|