You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 11_unit_testing.md
+11-15Lines changed: 11 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ mod tests {
16
16
17
17
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`.
18
18
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:
20
20
```rust
21
21
#[cfg(test)]
22
22
modunit_tests {
@@ -45,8 +45,7 @@ You might see something like the following:
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`
129
126
130
127
So far so good.
131
128
Let's add another testing scenario.
@@ -151,15 +148,15 @@ mod unit_tests {
151
148
We added another scenario for when the first byte is `0xfd` or the integer `253`.
152
149
In this case, we want to read the next two bytes to determine the input length.
153
150
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`.
157
154
158
155
You also might have noticed that we declared the same variables, `bytes` and `length`.
159
156
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.
160
157
It's easier to write it this way sometimes, rather than come up with new variable names for each scenario.
161
158
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:
163
160
164
161
```rust
165
162
#[cfg(test)]
@@ -190,14 +187,13 @@ mod unit_tests {
190
187
Let's add another scenario from a real world example.
191
188
We're going to use an example that was mentioned in the [Learn Me A Bitcoin](https://learnmeabitcoin.com/explorer/tx/52539a56b1eb890504b775171923430f0355eb836a57134ba598170a2f8980c1) tutorial site.
192
189
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.
194
192
Interesting.
195
193
196
194
Here is the raw transaction hex: https://mempool.space/api/tx/52539a56b1eb890504b775171923430f0355eb836a57134ba598170a2f8980c1/hex
197
195
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`
201
197
202
198
`fd` indicates that the input length comes from the next two bytes.
203
199
So the bytes, `0x20` and `0x4e` should evaluate to 20,000.
0 commit comments