Skip to content

Commit 8e71c74

Browse files
committed
Add test to validate deduplication of 'tm.tm_zone' string.
Signed-off-by: shamb0 <[email protected]>
1 parent 6666c44 commit 8e71c74

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

tests/pass-dep/libc/libc-time.rs

+51-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ fn main() {
88
test_localtime_r_gmt();
99
test_localtime_r_pst();
1010
test_localtime_r_epoch();
11+
test_localtime_r_verify_string_deduplication();
1112
// Architecture-specific tests.
1213
#[cfg(target_pointer_width = "32")]
1314
test_localtime_r_future_32b();
@@ -125,7 +126,7 @@ fn test_localtime_r_gmt() {
125126
fn test_localtime_r_pst() {
126127
let key = "TZ";
127128
env::set_var(key, "PST8PDT");
128-
const TIME_SINCE_EPOCH: libc::time_t = 1712475836;// 2024-04-07 07:43:56 GMT
129+
const TIME_SINCE_EPOCH: libc::time_t = 1712475836; // 2024-04-07 07:43:56 GMT
129130
let custom_time_ptr = &TIME_SINCE_EPOCH;
130131
let mut tm = create_empty_tm();
131132

@@ -276,3 +277,52 @@ fn test_localtime_r_future_32b() {
276277
assert!(ptr::eq(res, &mut tm));
277278
env::remove_var(key);
278279
}
280+
281+
fn test_localtime_r_verify_string_deduplication() {
282+
let key = "TZ";
283+
env::set_var(key, "PST8PDT");
284+
285+
// Two timestamps that are in the same timezone (PST/PDT).
286+
const TIME_SINCE_EPOCH_TZ_CONST1: libc::time_t = 1712475836; // 2024-04-07 07:43:56 GMT
287+
const TIME_SINCE_EPOCH_TZ_CONST2: libc::time_t = 1712575836; // 2024-04-08 11:23:56 GMT
288+
const TIME_SINCE_EPOCH_TZ_CONST3: libc::time_t = 1712675836; // 2024-04-09 11:23:56 GMT
289+
290+
let mut tm1 = create_empty_tm();
291+
let mut tm2 = create_empty_tm();
292+
let mut tm3 = create_empty_tm();
293+
294+
unsafe {
295+
let res1 = libc::localtime_r(&TIME_SINCE_EPOCH_TZ_CONST1, &mut tm1);
296+
let res2 = libc::localtime_r(&TIME_SINCE_EPOCH_TZ_CONST2, &mut tm2);
297+
let res3 = libc::localtime_r(&TIME_SINCE_EPOCH_TZ_CONST3, &mut tm3);
298+
299+
assert!(res1.is_null() == false, "localtime_r failed for first timestamp");
300+
assert!(res2.is_null() == false, "localtime_r failed for second timestamp");
301+
assert!(res3.is_null() == false, "localtime_r failed for third timestamp");
302+
303+
#[cfg(any(
304+
target_os = "linux",
305+
target_os = "macos",
306+
target_os = "freebsd",
307+
target_os = "android"
308+
))]
309+
{
310+
let tm_zone1 = std::ffi::CStr::from_ptr(tm1.tm_zone);
311+
let tm_zone2 = std::ffi::CStr::from_ptr(tm2.tm_zone);
312+
313+
println!("tz res1 :: {:#?}", tm1.tm_zone);
314+
println!("tz res2 :: {:#?}", tm2.tm_zone);
315+
println!("tz res3 :: {:#?}", tm3.tm_zone);
316+
317+
assert_eq!(
318+
tm_zone1, tm_zone2,
319+
"tm_zone strings are not equal, indicating different values."
320+
);
321+
322+
assert_eq!(
323+
tm1.tm_zone, tm2.tm_zone,
324+
"tm_zone pointers are not equal, string deduplication is not happening."
325+
);
326+
}
327+
}
328+
}

0 commit comments

Comments
 (0)