| 1 | // The libc crate on Windows doesn't have snprintf |
| 2 | #![cfg(not(windows))] |
| 3 | |
| 4 | use std::convert::{TryFrom, TryInto}; |
| 5 | use std::ffi::CString; |
| 6 | use std::mem::size_of; |
| 7 | use std::os::raw::c_char; |
| 8 | |
| 9 | use libc::snprintf; |
| 10 | |
| 11 | use sprintf::*; |
| 12 | |
| 13 | fn check_fmt<T: Printf>(fmt: &str, arg: T) { |
| 14 | let our_result = sprintf!(fmt, arg).unwrap(); |
| 15 | let mut buf = vec![0_u8; our_result.len() + 1]; |
| 16 | let cfmt = CString::new(fmt).unwrap(); |
| 17 | let clen: usize = unsafe { |
| 18 | snprintf( |
| 19 | buf.as_mut_ptr() as *mut c_char, |
| 20 | buf.len(), |
| 21 | cfmt.as_ptr(), |
| 22 | arg, |
| 23 | ) |
| 24 | } |
| 25 | .try_into() |
| 26 | .unwrap(); |
| 27 | buf.truncate(clen); // drop the final '\0', etc. |
| 28 | let c_result = String::from_utf8(buf).unwrap(); |
| 29 | assert_eq!(our_result, c_result); |
| 30 | } |
| 31 | |
| 32 | fn check_fmt_s(fmt: &str, arg: &str) { |
| 33 | let our_result = sprintf!(fmt, arg).unwrap(); |
| 34 | let mut buf = vec![0_u8; our_result.len() + 1]; |
| 35 | let cfmt = CString::new(fmt).unwrap(); |
| 36 | let carg = CString::new(arg).unwrap(); |
| 37 | let clen: usize = unsafe { |
| 38 | snprintf( |
| 39 | buf.as_mut_ptr() as *mut c_char, |
| 40 | buf.len(), |
| 41 | cfmt.as_ptr(), |
| 42 | carg.as_ptr(), |
| 43 | ) |
| 44 | } |
| 45 | .try_into() |
| 46 | .unwrap(); |
| 47 | buf.truncate(clen); // drop the final '\0', etc. |
| 48 | let c_result = String::from_utf8(buf).unwrap(); |
| 49 | assert_eq!(our_result, c_result); |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn test_int() { |
| 54 | check_fmt("%d", 12); |
| 55 | check_fmt("~%d~", 148); |
| 56 | check_fmt("00%dxx", -91232); |
| 57 | check_fmt("%x", -9232); |
| 58 | check_fmt("%X", 432); |
| 59 | check_fmt("%09X", 432); |
| 60 | check_fmt("%9X", 432); |
| 61 | check_fmt("%+9X", 492); |
| 62 | check_fmt("% #9x", 4589); |
| 63 | check_fmt("%2o", 4); |
| 64 | check_fmt("% 12d", -4); |
| 65 | check_fmt("% 12d", 48); |
| 66 | check_fmt("%ld", -4_i64); |
| 67 | check_fmt("%lX", -4_i64); |
| 68 | check_fmt("%ld", 48_i64); |
| 69 | check_fmt("%-8hd", -12_i16); |
| 70 | check_fmt("%llx", 0x0123456789abcdef_u64); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn test_float() { |
| 75 | check_fmt("%f", -46.38); |
| 76 | check_fmt("%012.3f", 1.2); |
| 77 | check_fmt("%012.3e", 1.7); |
| 78 | check_fmt("%e", 1e300); |
| 79 | check_fmt("%012.3g%%!", 2.6); |
| 80 | check_fmt("%012.5G", -2.69); |
| 81 | check_fmt("%+7.4f", 42.785); |
| 82 | check_fmt("{}% 7.4E", 493.12); |
| 83 | check_fmt("% 7.4E", -120.3); |
| 84 | check_fmt("%-10F", f64::INFINITY); |
| 85 | check_fmt("%+010F", f64::INFINITY); |
| 86 | check_fmt("%.1f", 999.99); |
| 87 | check_fmt("%.1f", 9.99); |
| 88 | check_fmt("%.1e", 9.99); |
| 89 | check_fmt("%.2f", 9.99); |
| 90 | check_fmt("%.2e", 9.99); |
| 91 | check_fmt("%.3f", 9.99); |
| 92 | check_fmt("%.3e", 9.99); |
| 93 | check_fmt("%.1g", 9.99); |
| 94 | check_fmt("%.1G", 9.99); |
| 95 | check_fmt("%.1f", 2.99); |
| 96 | check_fmt("%.1e", 2.99); |
| 97 | check_fmt("%.1g", 2.99); |
| 98 | check_fmt("%.1f", 2.599); |
| 99 | check_fmt("%.1e", 2.599); |
| 100 | check_fmt("%.1g", 2.599); |
| 101 | |
| 102 | // MacOS libc behaves differently from glibc for nan. glibc is the reference implementation. |
| 103 | if cfg!(target_env = "gnu") { |
| 104 | check_fmt("% f", f64::NAN); |
| 105 | check_fmt("%+f", f64::NAN); |
| 106 | } else { |
| 107 | assert_eq!(sprintf!("% f", f64::NAN).unwrap(), " nan"); |
| 108 | assert_eq!(sprintf!("%+f", f64::NAN).unwrap(), "+nan"); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn test_str() { |
| 114 | check_fmt_s("test %% with string: %s yay\n", "FOO"); |
| 115 | check_fmt_s( |
| 116 | "%s", |
| 117 | "testing with a slightly longer string to make sure it doesn't truncate", |
| 118 | ); |
| 119 | check_fmt("test char %c", '~'); |
| 120 | let c_string = CString::new("test").unwrap(); |
| 121 | check_fmt("%s", c_string.as_c_str()); |
| 122 | check_fmt("%s", c_string); |
| 123 | check_fmt_s("%4s", "A"); |
| 124 | check_fmt_s("%4s", "ð’€€"); // multi-byte character test (4 bytes) |
| 125 | check_fmt_s("%-4sX", "A"); |
| 126 | check_fmt_s("%-4sX", "ð’€€"); // multi-byte character test (4 bytes) |
| 127 | check_fmt_s("%1.3s", "ABCDEFG"); |
| 128 | check_fmt_s("%1.4s", "𒀀𒀀"); // multi-byte character test (4 bytes per char) |
| 129 | check_fmt_s("%8.4s", "ABCDEFG"); |
| 130 | |
| 131 | // glibc does not handle UTF-8 strings correctly when truncating, but we cannot produce malformed UTF-8 |
| 132 | // strings in Rust. Instead, we round down to the nearest character boundary. |
| 133 | assert_eq!(sprintf!("%1.1s", "𒀀𒀀𒀀").unwrap(), " "); |
| 134 | assert_eq!(sprintf!("%1.2s", "𒀀𒀀𒀀").unwrap(), " "); |
| 135 | assert_eq!(sprintf!("%1.3s", "𒀀𒀀𒀀").unwrap(), " "); |
| 136 | assert_eq!(sprintf!("%1.4s", "𒀀𒀀𒀀").unwrap(), "𒀀"); |
| 137 | assert_eq!(sprintf!("%1.5s", "𒀀𒀀𒀀").unwrap(), "𒀀"); |
| 138 | assert_eq!(sprintf!("%1.6s", "𒀀𒀀𒀀").unwrap(), "𒀀"); |
| 139 | assert_eq!(sprintf!("%1.7s", "𒀀𒀀𒀀").unwrap(), "𒀀"); |
| 140 | assert_eq!(sprintf!("%1.8s", "𒀀𒀀𒀀").unwrap(), "𒀀𒀀"); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn test_char() { |
| 145 | check_fmt("%c", 'x'); |
| 146 | check_fmt("%c", b'x'); |
| 147 | check_fmt("%c", b'x' as c_char); |
| 148 | check_fmt("%c", u16::try_from('x').unwrap()); |
| 149 | check_fmt("%c", u32::try_from('x').unwrap()); |
| 150 | check_fmt("%4c", 'A'); |
| 151 | check_fmt("%-4cX", 'A'); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn test_sanity() { |
| 156 | // u8 must not misinterpret bytes from multi-byte UTF-8 characters |
| 157 | let bytes = "∆".as_bytes(); |
| 158 | assert!(bytes.len() > 1); |
| 159 | assert_eq!(sprintf!("%c", bytes[0]), Err(PrintfError::WrongType)); |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn test_ptr() { |
| 164 | let buf: [u8; 4] = [0; 4]; |
| 165 | let ptr_const: *const u8 = buf.as_ptr(); |
| 166 | let ptr_mut: *mut u8 = ptr_const.cast_mut(); |
| 167 | |
| 168 | // pointer: expects usize and pointer to have the same size |
| 169 | assert_eq!(size_of::<usize>(), size_of::<*const u8>(),); |
| 170 | check_fmt("%p", ptr_const); |
| 171 | check_fmt("%p", ptr_mut); |
| 172 | |
| 173 | // numeric: works the same as libc if you use the correct length specifier |
| 174 | if size_of::<usize>() == size_of::<u64>() { |
| 175 | check_fmt("%llx", ptr_const); |
| 176 | check_fmt("%llx", ptr_mut); |
| 177 | check_fmt("%#llx", ptr_const); |
| 178 | check_fmt("%#llx", ptr_mut); |
| 179 | } else if size_of::<usize>() == size_of::<u32>() { |
| 180 | check_fmt("%x", ptr_const); |
| 181 | check_fmt("%x", ptr_mut); |
| 182 | check_fmt("%#x", ptr_const); |
| 183 | check_fmt("%#x", ptr_mut); |
| 184 | } |
| 185 | } |