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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use std::fmt;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Address {
address: u64,
}
impl Address {
pub fn new(address: u64) -> Self {
Address { address: address }
}
}
impl fmt::Binary for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.address.fmt(f)
}
}
impl fmt::Octal for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.address.fmt(f)
}
}
impl fmt::LowerHex for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.address.fmt(f)
}
}
impl fmt::UpperHex for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.address.fmt(f)
}
}
#[cfg(test)]
mod tests {
use super::Address;
#[test]
fn address_comparison() {
let addr1 = Address::new(5);
let addr2 = Address::new(3);
let addr3 = Address::new(5);
assert!(addr2 < addr1);
assert_eq!(addr1, addr3);
}
#[test]
fn binary_fmt() {
let a = Address::new(4);
assert_eq!("100", format!("{:b}", a));
}
#[test]
fn octal_fmt() {
let a = Address::new(10);
assert_eq!("12", format!("{:o}", a));
}
#[test]
fn lower_hex_fmt() {
let a = Address::new(0xc1);
assert_eq!("0xc1", format!("{:#x}", a));
assert_eq!(" 0xc1", format!("{:#6x}", a));
assert_eq!(" c1", format!("{:6x}", a));
assert_eq!("0000c1", format!("{:06x}", a));
}
#[test]
fn upper_hex_fmt() {
let a = Address::new(0xc1);
assert_eq!("0xC1", format!("{:#X}", a));
assert_eq!(" 0xC1", format!("{:#6X}", a));
assert_eq!(" C1", format!("{:6X}", a));
assert_eq!("0000C1", format!("{:06X}", a));
}
}