Skip to content

Commit a9b2c28

Browse files
committed
Added more unit tests
1 parent 0cf0de8 commit a9b2c28

File tree

3 files changed

+149
-12
lines changed

3 files changed

+149
-12
lines changed

src/lib/world/src/chunk_format.rs

+90-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ use std::io::Read;
1515
use tracing::{error, warn};
1616
use vanilla_chunk_format::BlockData;
1717

18-
#[cfg(test)]
19-
const BLOCKSFILE: &[u8] = &[0];
18+
// #[cfg(test)]
19+
// const BLOCKSFILE: &[u8] = &[0];
2020

2121
// If this file doesn't exist, you'll have to create it yourself. Download the 1.21.1 server from the
2222
// minecraft launcher, extract the blocks data (info here https://minecraft.wiki/w/Minecraft_Wiki:Projects/wiki.vg_merge/Data_Generators#Blocks_report)
2323
// , put the blocks.json file in the .etc folder, and run the blocks_parser.py script in the scripts
2424
// folder. This will generate the blockmappings.json file that is compressed with bzip2 and included
2525
// in the binary.
26-
#[cfg(not(test))]
26+
// #[cfg(not(test))]
2727
const BLOCKSFILE: &[u8] = include_bytes!("../../../../.etc/blockmappings.bz2");
2828

2929
lazy_static! {
@@ -748,3 +748,90 @@ impl Section {
748748
Ok(())
749749
}
750750
}
751+
752+
#[cfg(test)]
753+
mod tests {
754+
use super::*;
755+
756+
#[test]
757+
fn test_chunk_set_block() {
758+
let mut chunk = Chunk::new(0, 0, "overworld".to_string());
759+
let block = BlockData {
760+
name: "minecraft:stone".to_string(),
761+
properties: None,
762+
};
763+
chunk.set_block(0, 0, 0, block.clone()).unwrap();
764+
assert_eq!(chunk.get_block(0, 0, 0).unwrap(), block);
765+
}
766+
767+
#[test]
768+
fn test_chunk_fill() {
769+
let mut chunk = Chunk::new(0, 0, "overworld".to_string());
770+
let stone_block = BlockData {
771+
name: "minecraft:stone".to_string(),
772+
properties: None,
773+
};
774+
chunk.fill(stone_block.clone()).unwrap();
775+
for section in &chunk.sections {
776+
for (block, count) in &section.block_states.block_counts {
777+
assert_eq!(*block, stone_block);
778+
assert_eq!(count, &4096);
779+
}
780+
}
781+
}
782+
783+
#[test]
784+
fn test_section_fill() {
785+
let mut section = Section {
786+
y: 0,
787+
block_states: BlockStates {
788+
non_air_blocks: 0,
789+
block_data: PaletteType::Single(VarInt::from(0)),
790+
block_counts: HashMap::from([(BlockData::default(), 4096)]),
791+
},
792+
biome_states: BiomeStates {
793+
bits_per_biome: 0,
794+
data: vec![],
795+
palette: vec![VarInt::from(0)],
796+
},
797+
block_light: vec![255; 2048],
798+
sky_light: vec![255; 2048],
799+
};
800+
let stone_block = BlockData {
801+
name: "minecraft:stone".to_string(),
802+
properties: None,
803+
};
804+
section.fill(stone_block.clone()).unwrap();
805+
assert_eq!(
806+
section.block_states.block_data,
807+
PaletteType::Single(VarInt::from(1))
808+
);
809+
assert_eq!(
810+
section.block_states.block_counts.get(&stone_block).unwrap(),
811+
&4096
812+
);
813+
}
814+
815+
#[test]
816+
fn test_false_positive() {
817+
let mut chunk = Chunk::new(0, 0, "overworld".to_string());
818+
let block = BlockData {
819+
name: "minecraft:stone".to_string(),
820+
properties: None,
821+
};
822+
chunk.set_block(0, 0, 0, block.clone()).unwrap();
823+
assert_ne!(chunk.get_block(0, 1, 0).unwrap(), block);
824+
}
825+
826+
#[test]
827+
fn test_doesnt_fail() {
828+
let mut chunk = Chunk::new(0, 0, "overworld".to_string());
829+
let block = BlockData {
830+
name: "minecraft:stone".to_string(),
831+
properties: None,
832+
};
833+
assert!(chunk.set_block(0, 0, 0, block.clone()).is_ok());
834+
assert!(chunk.set_block(0, 0, 0, block.clone()).is_ok());
835+
assert!(chunk.get_block(0, 0, 0).is_ok());
836+
}
837+
}

src/lib/world_gen/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2024"
77
ferrumc-world = { workspace = true }
88
thiserror = { workspace = true }
99
noise = { workspace = true }
10+
rand = { workspace = true }
1011

1112
[lints]
1213
workspace = true

src/lib/world_gen/src/biomes/plains.rs

+58-9
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ impl BiomeGenerator for PlainsBiome {
4040
}
4141

4242
// Then generate some heights
43-
for chunk_x in 0..16 {
44-
for chunk_z in 0..16 {
45-
let global_x = x * 16 + chunk_x;
46-
let global_z = z * 16 + chunk_z;
47-
let height = noise.get_noise(f64::from(global_x), f64::from(global_z));
43+
for chunk_x in 0..16i64 {
44+
for chunk_z in 0..16i64 {
45+
let global_x = i64::from(x) * 16 + chunk_x;
46+
let global_z = i64::from(z) * 16 + chunk_z;
47+
let height = noise.get_noise(global_x as f64, global_z as f64);
4848
let height = (height * 64.0) as i32 + 64;
4949
heights.push((global_x, global_z, height));
5050
}
@@ -64,19 +64,19 @@ impl BiomeGenerator for PlainsBiome {
6464
for y in 0..height {
6565
if y + above_filled_sections <= 64 {
6666
chunk.set_block(
67-
global_x & 0xF,
67+
global_x as i32 & 0xF,
6868
y + above_filled_sections,
69-
global_z & 0xF,
69+
global_z as i32 & 0xF,
7070
BlockData {
7171
name: "minecraft:sand".to_string(),
7272
properties: None,
7373
},
7474
)?;
7575
} else {
7676
chunk.set_block(
77-
global_x & 0xF,
77+
global_x as i32 & 0xF,
7878
y + above_filled_sections,
79-
global_z & 0xF,
79+
global_z as i32 & 0xF,
8080
BlockData {
8181
name: "minecraft:grass_block".to_string(),
8282
properties: Some(BTreeMap::from([(
@@ -93,3 +93,52 @@ impl BiomeGenerator for PlainsBiome {
9393
Ok(chunk)
9494
}
9595
}
96+
97+
#[cfg(test)]
98+
mod test {
99+
use super::*;
100+
101+
#[test]
102+
fn test_is_ok() {
103+
let generator = PlainsBiome {};
104+
let noise = NoiseGenerator::new(0);
105+
assert!(generator.generate_chunk(0, 0, &noise).is_ok());
106+
}
107+
108+
#[test]
109+
fn test_random_chunk_generation() {
110+
let generator = PlainsBiome {};
111+
let noise = NoiseGenerator::new(0);
112+
for _ in 0..100 {
113+
let x = rand::random::<i32>();
114+
let z = rand::random::<i32>();
115+
assert!(generator.generate_chunk(x, z, &noise).is_ok());
116+
}
117+
}
118+
119+
#[test]
120+
fn test_very_high_coordinates() {
121+
let generator = PlainsBiome {};
122+
let noise = NoiseGenerator::new(0);
123+
assert!(
124+
generator
125+
.generate_chunk(1610612735, 1610612735, &noise)
126+
.is_ok()
127+
);
128+
assert!(
129+
generator
130+
.generate_chunk(-1610612735, -1610612735, &noise)
131+
.is_ok()
132+
);
133+
}
134+
135+
#[test]
136+
fn test_random_seeds() {
137+
for _ in 0..100 {
138+
let generator = PlainsBiome {};
139+
let seed = rand::random::<u64>();
140+
let noise = NoiseGenerator::new(seed);
141+
assert!(generator.generate_chunk(0, 0, &noise).is_ok());
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)