Skip to content

Commit fc13fc3

Browse files
committed
Use <= on 32-bit for some size assertions
Some of the assertions comparing the sizes of data structures to expected values have been using `==` (unconditionally). Of those, most but not all are mainly to safeguard against data structures growing larger. For those, this loosens the assertions on 32-bit targets to use `<=`, while still having them use `==` on 64-bit targets. The new `gix_testtools::size_ok` function is used to help with this. See the `size_ok` documentation comment for a rationale as to why it is done this way (and why this approach is not taken with assertions that seem intended to do more than keep the size from growing too large or without being noticed). This is to allow the following 18 tests to pass in the container (and hopefully on 32-bit systems in general): - gix-attributes::attributes search::size_of_outcome - gix-index extension::tree::tests::size_of_tree - gix-index size_of_entry - gix-index-tests::integrate index::size_of_entry - gix-negotiate::negotiate size_of_entry - gix-pack cache::delta::tests::size_of_pack_tree_item - gix-pack cache::delta::tests::size_of_pack_verify_data_structure - gix-pack cache::delta::tree::tests::size_of_pack_tree_item - gix-pack cache::delta::tree::tests::size_of_pack_verify_data_structure - gix-pack data::file::decode::entry::tests::size_of_decode_entry_outcome - gix-pack-tests::pack pack::data::output::size_of_count - gix-pack-tests::pack pack::data::output::size_of_entry - gix-pack-tests::pack pack::iter::size_of_entry - gix-ref raw::tests::size_of_reference - gix-revwalk::revwalk graph::commit::size_of_commit - gix::gix object::object_ref_size_in_memory - gix::gix object::oid_size_in_memory - gix::gix status::index_worktree::iter::item_size This splits a couple of test cases into two, where the added code would otherwise make them less readable. The duplicated tests described in GitoxideLabs#1685 are among those modified here, so this exacerbates that duplication, in that there is more duplicated code, but the duplicated test cases are still easy to resolve, once it is clear which modules they should be in. This adds "FIXME" comments to identify the duplication.
1 parent 77c3c59 commit fc13fc3

File tree

17 files changed

+184
-81
lines changed

17 files changed

+184
-81
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gix-attributes/tests/search/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use gix_attributes::{
66
AssignmentRef, NameRef, StateRef,
77
};
88
use gix_glob::pattern::Case;
9+
use gix_testtools::size_ok;
910

1011
mod specials {
1112
use std::path::Path;
@@ -268,10 +269,11 @@ fn given_attributes_are_made_available_in_given_order() -> crate::Result {
268269

269270
#[test]
270271
fn size_of_outcome() {
271-
assert_eq!(
272-
std::mem::size_of::<Outcome>(),
273-
840,
274-
"it's quite big, shouldn't change without us noticing"
272+
let actual = std::mem::size_of::<Outcome>();
273+
let expected = 840;
274+
assert!(
275+
size_ok(actual, expected),
276+
"it's quite big, shouldn't change without us noticing: {actual} <~ {expected}"
275277
);
276278
}
277279

gix-index/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,8 @@ rustix = { version = "0.38.20", default-features = false, features = [
5858
] }
5959
libc = { version = "0.2.149" }
6060

61+
[dev-dependencies]
62+
gix-testtools = { path = "../tests/tools" }
63+
6164
[package.metadata.docs.rs]
6265
features = ["document-features", "serde"]

gix-index/src/extension/tree/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ mod write;
1313

1414
#[cfg(test)]
1515
mod tests {
16+
use gix_testtools::size_ok;
1617

1718
#[test]
1819
fn size_of_tree() {
19-
assert_eq!(std::mem::size_of::<crate::extension::Tree>(), 88);
20+
let actual = std::mem::size_of::<crate::extension::Tree>();
21+
let expected = 88;
22+
assert!(
23+
size_ok(actual, expected),
24+
"the size of this structure should not change unexpectedly: {actual} <~ {expected}"
25+
);
2026
}
2127
}

gix-index/src/lib.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,35 @@ pub(crate) mod util {
238238
}
239239
}
240240

241-
#[test]
242-
fn size_of_entry() {
243-
assert_eq!(std::mem::size_of::<crate::Entry>(), 80);
241+
// FIXME: Probably remove these in favor of the equivalent tests in `gix-index/tests/index/mod.rs`.
242+
#[cfg(test)]
243+
mod tests {
244+
use gix_testtools::size_ok;
244245

245-
// the reason we have our own time is half the size.
246-
assert_eq!(std::mem::size_of::<crate::entry::stat::Time>(), 8);
247-
assert_eq!(std::mem::size_of::<filetime::FileTime>(), 16);
246+
#[test]
247+
fn size_of_entry() {
248+
let actual = std::mem::size_of::<crate::Entry>();
249+
let expected = 80;
250+
assert!(
251+
size_ok(actual, expected),
252+
"the size of this structure should not change unexpectedly: {actual} <~ {expected}"
253+
);
254+
}
255+
256+
#[test]
257+
fn size_of_entry_time() {
258+
// The reason we have our own time is that it is half the size.
259+
let ent_actual = std::mem::size_of::<crate::entry::stat::Time>();
260+
let ent_expected = 8;
261+
assert!(
262+
size_ok(ent_actual, ent_expected),
263+
"the size of this structure should not change unexpectedly: {ent_actual} <~ {ent_expected}"
264+
);
265+
let ft_actual = std::mem::size_of::<filetime::FileTime>();
266+
let ft_expected = 16;
267+
assert!(
268+
size_ok(ft_actual, ft_expected),
269+
"we will want to know if the size of this structure changes: {ft_actual} <~ {ft_expected}"
270+
);
271+
}
248272
}

gix-index/tests/index/mod.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
use std::path::{Path, PathBuf};
2-
3-
use gix_hash::ObjectId;
4-
51
mod access;
62
mod entry;
73
mod file;
84
mod fs;
95
mod init;
106

7+
use std::path::{Path, PathBuf};
8+
9+
use gix_hash::ObjectId;
10+
use gix_testtools::size_ok;
11+
1112
pub fn hex_to_id(hex: &str) -> ObjectId {
1213
ObjectId::from_hex(hex.as_bytes()).expect("40 bytes hex")
1314
}
@@ -25,11 +26,29 @@ pub fn loose_file_path(name: &str) -> PathBuf {
2526

2627
#[test]
2728
fn size_of_entry() {
28-
assert_eq!(std::mem::size_of::<gix_index::Entry>(), 80);
29+
let actual = std::mem::size_of::<gix_index::Entry>();
30+
let expected = 80;
31+
assert!(
32+
size_ok(actual, expected),
33+
"the size of this structure should not change unexpectedly: {actual} <~ {expected}"
34+
);
35+
}
2936

30-
// the reason we have our own time is half the size.
31-
assert_eq!(std::mem::size_of::<gix_index::entry::stat::Time>(), 8);
32-
assert_eq!(std::mem::size_of::<filetime::FileTime>(), 16);
37+
#[test]
38+
fn size_of_entry_time() {
39+
// The reason we have our own time is that it is half the size.
40+
let ent_actual = std::mem::size_of::<gix_index::entry::stat::Time>();
41+
let ent_expected = 8;
42+
assert!(
43+
size_ok(ent_actual, ent_expected),
44+
"the size of this structure should not change unexpectedly: {ent_actual} <~ {ent_expected}"
45+
);
46+
let ft_actual = std::mem::size_of::<filetime::FileTime>();
47+
let ft_expected = 16;
48+
assert!(
49+
size_ok(ft_actual, ft_expected),
50+
"we will want to know if the size of this structure changes: {ft_actual} <~ {ft_expected}"
51+
);
3352
}
3453

3554
enum Fixture {

gix-negotiate/tests/negotiate.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use gix_testtools::Result;
1+
use gix_testtools::{size_ok, Result};
22

33
mod window_size {
44
use gix_negotiate::window_size;
@@ -38,9 +38,10 @@ mod baseline;
3838

3939
#[test]
4040
fn size_of_entry() {
41-
assert_eq!(
42-
std::mem::size_of::<gix_revwalk::graph::Commit<gix_negotiate::Metadata>>(),
43-
56,
44-
"we may keep a lot of these, so let's not let them grow unnoticed"
41+
let actual = std::mem::size_of::<gix_revwalk::graph::Commit<gix_negotiate::Metadata>>();
42+
let expected = 56;
43+
assert!(
44+
size_ok(actual, expected),
45+
"we may keep a lot of these, so let's not let them grow unnoticed: {actual} <~ {expected}"
4546
);
4647
}

gix-pack/src/cache/delta/mod.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,24 @@ mod tree;
2323

2424
pub use tree::{Item, Tree};
2525

26+
// FIXME: Probably remove this pair of tests or the equivalent pair in `gix-pack/src/cache/delta/tree.rs`.
2627
#[cfg(test)]
2728
mod tests {
29+
use super::Item;
30+
use gix_testtools::size_ok;
2831

2932
#[test]
3033
fn size_of_pack_tree_item() {
31-
use super::Item;
32-
assert_eq!(std::mem::size_of::<[Item<()>; 7_500_000]>(), 300_000_000);
34+
let actual = std::mem::size_of::<[Item<()>; 7_500_000]>();
35+
let expected = 300_000_000;
36+
assert!(
37+
size_ok(actual, expected),
38+
"we don't want these to grow unnoticed: {actual} <~ {expected}"
39+
);
3340
}
3441

3542
#[test]
3643
fn size_of_pack_verify_data_structure() {
37-
use super::Item;
3844
pub struct EntryWithDefault {
3945
_index_entry: crate::index::Entry,
4046
_kind: gix_object::Kind,
@@ -45,6 +51,11 @@ mod tests {
4551
_level: u16,
4652
}
4753

48-
assert_eq!(std::mem::size_of::<[Item<EntryWithDefault>; 7_500_000]>(), 840_000_000);
54+
let actual = std::mem::size_of::<[Item<EntryWithDefault>; 7_500_000]>();
55+
let expected = 840_000_000;
56+
assert!(
57+
size_ok(actual, expected),
58+
"we don't want these to grow unnoticed: {actual} <~ {expected}"
59+
);
4960
}
5061
}

gix-pack/src/cache/delta/tree.rs

+31-17
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,39 @@ mod tests {
226226
}
227227
}
228228

229-
#[test]
230-
fn size_of_pack_tree_item() {
231-
use super::Item;
232-
assert_eq!(std::mem::size_of::<[Item<()>; 7_500_000]>(), 300_000_000);
233-
}
229+
// FIXME: Probably remove this pair of tests or the equivalent pair in `gix-pack/src/cache/delta/mod.rs`.
230+
mod size {
231+
use super::super::Item;
232+
use gix_testtools::size_ok;
234233

235-
#[test]
236-
fn size_of_pack_verify_data_structure() {
237-
use super::Item;
238-
pub struct EntryWithDefault {
239-
_index_entry: crate::index::Entry,
240-
_kind: gix_object::Kind,
241-
_object_size: u64,
242-
_decompressed_size: u64,
243-
_compressed_size: u64,
244-
_header_size: u16,
245-
_level: u16,
234+
#[test]
235+
fn size_of_pack_tree_item() {
236+
let actual = std::mem::size_of::<[Item<()>; 7_500_000]>();
237+
let expected = 300_000_000;
238+
assert!(
239+
size_ok(actual, expected),
240+
"we don't want these to grow unnoticed: {actual} <~ {expected}"
241+
);
246242
}
247243

248-
assert_eq!(std::mem::size_of::<[Item<EntryWithDefault>; 7_500_000]>(), 840_000_000);
244+
#[test]
245+
fn size_of_pack_verify_data_structure() {
246+
pub struct EntryWithDefault {
247+
_index_entry: crate::index::Entry,
248+
_kind: gix_object::Kind,
249+
_object_size: u64,
250+
_decompressed_size: u64,
251+
_compressed_size: u64,
252+
_header_size: u16,
253+
_level: u16,
254+
}
255+
256+
let actual = std::mem::size_of::<[Item<EntryWithDefault>; 7_500_000]>();
257+
let expected = 840_000_000;
258+
assert!(
259+
size_ok(actual, expected),
260+
"we don't want these to grow unnoticed: {actual} <~ {expected}"
261+
);
262+
}
249263
}
250264
}

gix-pack/src/data/file/decode/entry.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,15 @@ impl File {
420420
#[cfg(test)]
421421
mod tests {
422422
use super::*;
423+
use gix_testtools::size_ok;
423424

424425
#[test]
425426
fn size_of_decode_entry_outcome() {
426-
assert_eq!(
427-
std::mem::size_of::<Outcome>(),
428-
32,
429-
"this shouldn't change without use noticing as it's returned a lot"
427+
let actual = std::mem::size_of::<Outcome>();
428+
let expected = 32;
429+
assert!(
430+
size_ok(actual, expected),
431+
"this shouldn't change without use noticing as it's returned a lot: {actual} <~ {expected}"
430432
);
431433
}
432434
}

gix-pack/tests/pack/data/output/mod.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
use std::{path::PathBuf, sync::Arc};
22

33
use gix_pack::data::output;
4+
use gix_testtools::size_ok;
45

56
#[test]
67
fn size_of_entry() {
7-
assert_eq!(
8-
std::mem::size_of::<output::Entry>(),
9-
80,
10-
"The size of the structure shouldn't change unexpectedly"
8+
let actual = std::mem::size_of::<output::Entry>();
9+
let expected = 80;
10+
assert!(
11+
size_ok(actual, expected),
12+
"The size of the structure shouldn't change unexpectedly: {actual} <~ {expected}"
1113
);
1214
}
1315

1416
#[test]
1517
fn size_of_count() {
16-
assert_eq!(
17-
std::mem::size_of::<output::Count>(),
18-
56,
19-
"The size of the structure shouldn't change unexpectedly"
18+
let actual = std::mem::size_of::<output::Count>();
19+
let expected = 56;
20+
assert!(
21+
size_ok(actual, expected),
22+
"The size of the structure shouldn't change unexpectedly: {actual} <~ {expected}"
2023
);
2124
}
2225

gix-pack/tests/pack/iter.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use gix_odb::pack;
2+
use gix_testtools::size_ok;
23

34
#[test]
45
fn size_of_entry() {
5-
assert_eq!(
6-
std::mem::size_of::<pack::data::input::Entry>(),
7-
104,
8-
"let's keep the size in check as we have many of them"
6+
let actual = std::mem::size_of::<pack::data::input::Entry>();
7+
let expected = 104;
8+
assert!(
9+
size_ok(actual, expected),
10+
"let's keep the size in check as we have many of them: {actual} <~ {expected}"
911
);
1012
}
1113

gix-ref/src/raw.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,15 @@ mod access {
9494
#[cfg(test)]
9595
mod tests {
9696
use super::*;
97+
use gix_testtools::size_ok;
9798

9899
#[test]
99100
fn size_of_reference() {
100-
assert_eq!(
101-
std::mem::size_of::<Reference>(),
102-
80,
103-
"let's not let it change size undetected"
101+
let actual = std::mem::size_of::<Reference>();
102+
let expected = 80;
103+
assert!(
104+
size_ok(actual, expected),
105+
"let's not let it change size undetected: {actual} <~ {expected}"
104106
);
105107
}
106108
}

gix-revwalk/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ gix-commitgraph = { version = "^0.25.0", path = "../gix-commitgraph" }
2323

2424
thiserror = "2.0.0"
2525
smallvec = "1.10.0"
26+
27+
[dev-dependencies]
28+
gix-testtools = { path = "../tests/tools" }

gix-revwalk/tests/revwalk.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
mod graph {
22
mod commit {
3+
use gix_testtools::size_ok;
4+
35
#[test]
46
fn size_of_commit() {
5-
assert_eq!(
6-
std::mem::size_of::<gix_revwalk::graph::Commit<()>>(),
7-
48,
8-
"We might see quite a lot of these, so they shouldn't grow unexpectedly"
7+
let actual = std::mem::size_of::<gix_revwalk::graph::Commit<()>>();
8+
let expected = 48;
9+
assert!(
10+
size_ok(actual, expected),
11+
"We might see quite a lot of these, so they shouldn't grow unexpectedly: {actual} <~ {expected}"
912
);
1013
}
1114
}

0 commit comments

Comments
 (0)