1 use wasmtime::Result;
2 use wasmtime_environ::collections::TryString;
3 use wasmtime_fuzzing::oom::OomTest;
4 
5 #[test]
try_string_with_capacity() -> Result<()>6 fn try_string_with_capacity() -> Result<()> {
7     OomTest::new().test(|| {
8         let _s = TryString::with_capacity(100)?;
9         Ok(())
10     })
11 }
12 
13 #[test]
try_string_reserve() -> Result<()>14 fn try_string_reserve() -> Result<()> {
15     OomTest::new().test(|| {
16         let mut s = TryString::new();
17         s.reserve(10)?;
18         Ok(())
19     })
20 }
21 
22 #[test]
try_string_reserve_exact() -> Result<()>23 fn try_string_reserve_exact() -> Result<()> {
24     OomTest::new().test(|| {
25         let mut s = TryString::new();
26         s.reserve_exact(3)?;
27         Ok(())
28     })
29 }
30 
31 #[test]
try_string_push() -> Result<()>32 fn try_string_push() -> Result<()> {
33     OomTest::new().test(|| {
34         let mut s = TryString::new();
35         s.push('c')?;
36         Ok(())
37     })
38 }
39 
40 #[test]
try_string_push_str() -> Result<()>41 fn try_string_push_str() -> Result<()> {
42     OomTest::new().test(|| {
43         let mut s = TryString::new();
44         s.push_str("hello")?;
45         Ok(())
46     })
47 }
48 
49 #[test]
try_string_shrink_to_fit() -> Result<()>50 fn try_string_shrink_to_fit() -> Result<()> {
51     OomTest::new().test(|| {
52         // len == cap == 0
53         let mut s = TryString::new();
54         s.shrink_to_fit()?;
55 
56         // len == 0 < cap
57         let mut s = TryString::with_capacity(4)?;
58         s.shrink_to_fit()?;
59 
60         // 0 < len < cap
61         let mut s = TryString::with_capacity(4)?;
62         s.push('a')?;
63         s.shrink_to_fit()?;
64 
65         // 0 < len == cap
66         let mut s = TryString::new();
67         s.reserve_exact(2)?;
68         s.push('a')?;
69         s.push('a')?;
70         s.shrink_to_fit()?;
71 
72         Ok(())
73     })
74 }
75 
76 #[test]
try_string_into_boxed_str() -> Result<()>77 fn try_string_into_boxed_str() -> Result<()> {
78     OomTest::new().test(|| {
79         // len == cap == 0
80         let s = TryString::new();
81         let _ = s.into_boxed_str()?;
82 
83         // len == 0 < cap
84         let s = TryString::with_capacity(4)?;
85         let _ = s.into_boxed_str()?;
86 
87         // 0 < len < cap
88         let mut s = TryString::with_capacity(4)?;
89         s.push('a')?;
90         let _ = s.into_boxed_str()?;
91 
92         // 0 < len == cap
93         let mut s = TryString::new();
94         s.reserve_exact(2)?;
95         s.push('a')?;
96         s.push('a')?;
97         let _ = s.into_boxed_str()?;
98 
99         Ok(())
100     })
101 }
102