forked from starkware-libs/cairo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.cairo
More file actions
62 lines (59 loc) · 1.95 KB
/
Copy pathtesting.cairo
File metadata and controls
62 lines (59 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Measurement of gas consumption for testing purpose.
//!
//! This module provides the `get_available_gas` function, useful for asserting the amount of gas
//! consumed by a particular operation or function call.
//! By calling `get_available_gas` before and after the operation, you can calculate the exact
//! amount of gas used.
use crate::gas::GasBuiltin;
/// Returns the amount of gas available in the `GasBuiltin`.
///
/// Useful for asserting that a certain amount of gas was consumed.
/// Note: The actual gas consumption observed by calls to `get_available_gas` is only exact
/// immediately before calls to `withdraw_gas`.
///
/// # Examples
///
/// ```
/// use core::testing::get_available_gas;
///
/// fn gas_heavy_function() {
/// // ... some gas-intensive code
/// }
///
/// fn test_gas_consumption() {
/// let gas_before = get_available_gas();
/// // Making sure `gas_before` is exact.
/// core::gas::withdraw_gas().unwrap();
///
/// gas_heavy_function();
///
/// let gas_after = get_available_gas();
/// // Making sure `gas_after` is exact
/// core::gas::withdraw_gas().unwrap();
///
/// assert!(gas_before - gas_after < 100_000);
/// }
/// ```
pub extern fn get_available_gas() -> u128 implicits(GasBuiltin) nopanic;
/// Returns the amount of gas available in the `GasBuiltin`, as well as the amount of gas unused in
/// the local wallet.
///
/// Useful for asserting that a certain amount of gas was used.
/// Note: This function call costs exactly `2300` gas, so this may be ignored in calculations.
/// # Examples
///
/// ```
/// use core::testing::get_unspent_gas;
///
/// fn gas_heavy_function() {
/// // ... some gas-intensive code
/// }
///
/// fn test_gas_consumption() {
/// let gas_before = get_unspent_gas();
/// gas_heavy_function();
/// let gas_after = get_unspent_gas();
/// assert!(gas_before - gas_after < 100_000);
/// }
/// ```
pub extern fn get_unspent_gas() -> u128 implicits(GasBuiltin) nopanic;