Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for OP_EQUALVERIFY #317

Merged
merged 7 commits into from
Aug 9, 2018
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 52 additions & 21 deletions BitcoinCashKitTests/OpCodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,26 @@ class OpCodeTests: XCTestCase {
}

func testOp0() {
let op0 = OpCode.OP_0
let opcode = OpCode.OP_0
do {
try op0.execute(context)
try opcode.execute(context)
let num = try context.number(at: -1)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(num, 0)
} catch let error {
fail(with: opcode, error: error)
}
}

func testOp1Negate() {
let opcode = OpCode.OP_1NEGATE
do {
try opcode.execute(context)
let num = try context.number(at: -1)
XCTAssertEqual(context.stack.count, 1)
XCTAssertEqual(num, -1)
} catch let error {
XCTFail("OP_0 execution should not fail.\nError: \(error)")
fail(with: opcode, error: error)
}
}

Expand Down Expand Up @@ -76,7 +88,7 @@ class OpCodeTests: XCTestCase {
XCTAssertEqual(num, expectedNumber, "\(opcode.name)(\(opcode.value) execution test.")
XCTAssertEqual(context.stack.count, i + 1)
} catch let error {
XCTFail("\(opcode.name)(\(opcode.value) execution should not fail.\nError: \(error)")
fail(with: opcode, error: error)
}
}
}
Expand All @@ -96,18 +108,13 @@ class OpCodeTests: XCTestCase {
XCTAssertEqual(context.stack.count, stackCountAtFirst + 1)
try opcode.execute(context)
XCTAssertEqual(context.stack.count, stackCountAtFirst, "\(opcode.name)(\(String(format: "%02x", opcode.value)) execution test.")
} catch {
XCTFail("\(opcode.name)(\(opcode.value) execution should not fail.")
} catch let error {
fail(with: opcode, error: error)
}

// OP_VERIFY fail
do {
context.pushToStack(false)
try opcode.execute(context)
XCTFail("\(opcode.name)(\(opcode.value) execution should throw error.")
} catch {
// do nothing equal success
}
context.pushToStack(false)
XCTAssertThrowsError(try opcode.execute(context))
}

func testOpDuplicate() {
Expand All @@ -126,20 +133,41 @@ class OpCodeTests: XCTestCase {
let dataOnTop: Data = context.stack.last!
try opcode.execute(context)
XCTAssertEqual(context.stack.count, stackCountAtFirst + 1, "\(opcode.name)(\(String(format: "%02x", opcode.value)) test: One data should be added to stack.")
XCTAssertEqual(context.stack.dropLast().map(Data.init), stackSnapShot, "\(opcode.name)(\(String(format: "%02x", opcode.value)) test: The data except the top should be the same after the execution.")
XCTAssertEqual(context.stack.dropLast().map({Data.init($0)}), stackSnapShot, "\(opcode.name)(\(String(format: "%02x", opcode.value)) test: The data except the top should be the same after the execution.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data($0) is better.
Explicitly calling .init() should be avoided.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this line?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I committed this line for my wrong.

But my Xcode shows this error.
Ambiguous use of 'init'
My environment is Xcode Version 10.0 beta 5 (10L221o)

What should I do?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the same issue as here.
I think this is because you are using Xcode beta version.
https://bugs.swift.org/browse/SR-3842

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @akifuj said, you'd better write like this.
.map({Data($0)})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking link.
I fixed as .map { Data($0) }

XCTAssertEqual(context.stack.last!, dataOnTop, "\(opcode.name)(\(String(format: "%02x", opcode.value)) test: The data on top should be copied and pushed.")
} catch {
XCTFail("\(opcode.name)(\(opcode.value) execution should not fail.")
} catch let error {
fail(with: opcode, error: error)
}

// OP_DUP fail
context.resetStack()
XCTAssertEqual(context.stack.count, 0)
XCTAssertThrowsError(try opcode.execute(context))
}

func testOpEqualVerify() {
let opcode = OpCode.OP_EQUALVERIFY
// OP_EQUALVERIFY success
do {
context.resetStack()
XCTAssertEqual(context.stack.count, 0)
try context.pushToStack(1)
try context.pushToStack(1)
XCTAssertEqual(context.stack.count, 2)
try opcode.execute(context)
XCTFail("\(opcode.name)(\(opcode.value) execution should throw error when stack is empty.")
} catch {
// do nothing equal success
XCTAssertEqual(context.stack.count, 0)
} catch let error {
fail(with: opcode, error: error)
}

// OP_EQUALVERIFY fail
do {
context.resetStack()
try context.pushToStack(1)
try context.pushToStack(3)
XCTAssertEqual(context.stack.count, 2)
XCTAssertThrowsError(try opcode.execute(context))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

XCTAssertEqual(context.stack.count, 1)
} catch let error {
fail(with: opcode, error: error)
}
}
}
Expand All @@ -159,3 +187,6 @@ private func pushRandomDataOnStack(_ context: ScriptExecutionContext) {
}
}

private func fail(with opCode: OpCodeProtocol, error: Error) {
XCTFail("\(opCode.name)(\(opCode.value)) execution should not fail.\nError: \(error)")
}