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
63
64
65
66
67
68
69
70
71
72
use crate::error::ErrorCode;
use anchor_lang::require;
pub fn add_delta(x: u64, y: i64) -> Result<u64, anchor_lang::error::Error> {
let z: u64;
if y < 0 {
z = x - (-y as u64);
require!(z < x, ErrorCode::LS);
} else {
z = x + (y as u64);
require!(z >= x, ErrorCode::LA);
}
Ok(z)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one_plus_zero() {
assert_eq!(add_delta(1, 0).unwrap(), 1);
}
#[test]
fn one_plus_minus_one() {
assert_eq!(add_delta(1, -1).unwrap(), 0);
}
#[test]
fn one_plus_one() {
assert_eq!(add_delta(1, 1).unwrap(), 2);
}
#[test]
#[should_panic]
fn u64_max_plus_one_overflows() {
add_delta(u64::MAX, 1).unwrap();
}
#[test]
#[should_panic]
fn two_pow_64_minus_fifteen_plus_fifteen_overflows() {
add_delta((u128::pow(2, 64) - 15) as u64, 15).unwrap();
}
#[test]
#[should_panic]
fn zero_minus_one_underflows() {
add_delta(0, -1).unwrap();
}
#[test]
#[should_panic]
fn three_minus_four_underflows() {
add_delta(3, -4).unwrap();
}
}