forked from yenom/BitcoinKit
-
Notifications
You must be signed in to change notification settings - Fork 6
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
usatie
merged 7 commits into
BitcoinCashKit:master
from
s2mr:add-tests/opcode/op_equalverify
Aug 9, 2018
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5a48ea8
add tests for OP_1NEGATE
s2mr 66e12c4
increase usablity
s2mr 856716d
add tests for OP_EQUALVERIFY
s2mr f3c5d1b
fixed exists tests
s2mr fa887b0
fixed for Data.init(-zsh)
s2mr 8de6d0d
fixed tests for checking threw error
s2mr e572ecf
fixed for reviewing
s2mr File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
} | ||
|
||
|
@@ -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) | ||
} | ||
} | ||
} | ||
|
@@ -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() { | ||
|
@@ -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.") | ||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
} | ||
} | ||
|
@@ -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)") | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)})
There was a problem hiding this comment.
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) }