Skip to content

Commit f734373

Browse files
committed
Minor text edits
1 parent 4efad54 commit f734373

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

11_unit_testing.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod tests {
1616

1717
The `#[cfg(test)]` annotation on the tests module tells Rust to compile and run the test code only when you run `cargo test`, not when you run `cargo build`.
1818

19-
So let's start setting up our tests:
19+
So let's start setting up our tests by adding the following after the `main` function:
2020
```rust
2121
#[cfg(test)]
2222
mod unit_tests {
@@ -45,8 +45,7 @@ You might see something like the following:
4545
Running unittests src/main.rs (target/debug/deps/transaction_decoder_11-283cd8d491efdffc)
4646

4747
running 1 test
48-
test unit_tests::test_reading_compact_size ...
49-
ok
48+
test unit_tests::test_reading_compact_size ... ok
5049

5150
test result: ok.
5251
1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
@@ -82,8 +81,7 @@ If you run `cargo test`, you should see the test failures:
8281
Running unittests src/main.rs (target/debug/deps/transaction_decoder_11-283cd8d491efdffc)
8382

8483
running 1 test
85-
test unit_tests::test_reading_compact_size ...
86-
FAILED
84+
test unit_tests::test_reading_compact_size ... FAILED
8785

8886
failures:
8987

@@ -124,8 +122,7 @@ mod unit_tests {
124122
}
125123
```
126124

127-
Now if you run `cargo test` again, you will see that all tests pass with the output line at the end, `test result: ok.
128-
1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s`
125+
Now if you run `cargo test` again, you will see that all tests pass with the output line at the end, `test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s`
129126

130127
So far so good.
131128
Let's add another testing scenario.
@@ -151,15 +148,15 @@ mod unit_tests {
151148
We added another scenario for when the first byte is `0xfd` or the integer `253`.
152149
In this case, we want to read the next two bytes to determine the input length.
153150
So if we pass this slice in, we should get the correct length.
154-
What is the length represented by the bytes `0` and `1`? Well we can use base math to confirm.
155-
Remember, bytes are in little endian, so the `0` is in the 0th position and the `1` is in the first position.
156-
We can ignore the 0 and the other value is `1*256_u64.pow(1)` or just `256_u64`.
151+
What is the length represented by the bytes `0` and `1`?
152+
Well we can use base math to confirm.
153+
Remember, bytes are in little endian, so the `0` is in the least significant position and the `1` is in the most significant position: `1*256_u64.pow(1) + 0*256_u64.pow(0)` or just `256_u64`.
157154

158155
You also might have noticed that we declared the same variables, `bytes` and `length`.
159156
This is called [*shadowing*](https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing) in Rust as the second declaration will "*overshadow*" the first.
160157
It's easier to write it this way sometimes, rather than come up with new variable names for each scenario.
161158

162-
Let's add a few more scenarios testing the other arms of the match statement:
159+
Let's add a few more scenarios to test the other arms of the match statement:
163160

164161
```rust
165162
#[cfg(test)]
@@ -190,14 +187,13 @@ mod unit_tests {
190187
Let's add another scenario from a real world example.
191188
We're going to use an example that was mentioned in the [Learn Me A Bitcoin](https://learnmeabitcoin.com/explorer/tx/52539a56b1eb890504b775171923430f0355eb836a57134ba598170a2f8980c1) tutorial site.
192189
It's a [transaction](https://mempool.space/tx/52539a56b1eb890504b775171923430f0355eb836a57134ba598170a2f8980c1) with 20,000 inputs which was confirmed in 2015.
193-
840,000 vbytes large and paid 0 fees! It was all 0 inputs and 0 output as well so no amount of Bitcoin was transferred.
190+
It is 840,000 vbytes large and paid 0 fees!
191+
It was all 0 inputs and 0 output as well so no amount of Bitcoin was transferred.
194192
Interesting.
195193

196194
Here is the raw transaction hex: https://mempool.space/api/tx/52539a56b1eb890504b775171923430f0355eb836a57134ba598170a2f8980c1/hex
197195

198-
If we look at the first few bytes, we can see the version followed by `fd`.
199-
200-
`01000000fd204e`
196+
If we look at the first few bytes, we can see the version followed by `fd`: `01000000fd204e`
201197

202198
`fd` indicates that the input length comes from the next two bytes.
203199
So the bytes, `0x20` and `0x4e` should evaluate to 20,000.

0 commit comments

Comments
 (0)