Skip to content

Commit 0f60a37

Browse files
Implement terrain resource tests
1 parent 51d86f6 commit 0f60a37

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/client/terrain/resources.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,59 @@ impl ChunkManager {
110110
self.get_chunk(chunk_position)
111111
}
112112
}
113+
114+
#[cfg(test)]
115+
mod tests {
116+
use super::*;
117+
118+
#[test]
119+
fn test_chunk_manager_new() {
120+
let chunk_manager = ChunkManager::new();
121+
assert!(chunk_manager.chunks.is_empty());
122+
}
123+
124+
#[test]
125+
fn test_instantiate_chunks() {
126+
let position = Vec3::new(0.0, 0.0, 0.0);
127+
let render_distance = 2;
128+
let chunks = ChunkManager::instantiate_chunks(position, render_distance);
129+
assert_eq!(chunks.len(), (render_distance * render_distance * render_distance) as usize);
130+
}
131+
132+
#[test]
133+
fn test_insert_chunks() {
134+
let mut chunk_manager = ChunkManager::new();
135+
let position = Vec3::new(0.0, 0.0, 0.0);
136+
let render_distance = 2;
137+
let chunks = ChunkManager::instantiate_chunks(position, render_distance);
138+
139+
chunk_manager.insert_chunks(chunks);
140+
assert_eq!(chunk_manager.chunks.len(), (render_distance * render_distance * render_distance) as usize);
141+
}
142+
143+
#[test]
144+
fn test_set_and_get_chunk() {
145+
let mut chunk_manager = ChunkManager::new();
146+
let position = Vec3::new(0.0, 0.0, 0.0);
147+
let chunk = Chunk::new(position);
148+
149+
chunk_manager.set_chunk(position, chunk.clone());
150+
let retrieved_chunk = chunk_manager.get_chunk(position).unwrap();
151+
assert_eq!(retrieved_chunk.position, chunk.position);
152+
}
153+
154+
#[test]
155+
fn test_set_and_get_block() {
156+
let mut chunk_manager = ChunkManager::new();
157+
let position = Vec3::new(0.0, 0.0, 0.0);
158+
let chunk = Chunk::new(position);
159+
160+
chunk_manager.set_chunk(position, chunk);
161+
let block_position = Vec3::new(1.0, 1.0, 1.0);
162+
let block_id = BlockId::Stone;
163+
164+
chunk_manager.set_block(block_position, block_id);
165+
let retrieved_block = chunk_manager.get_block(block_position).unwrap();
166+
assert_eq!(retrieved_block, block_id);
167+
}
168+
}

src/client/terrain/util/chunk.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pub const PADDED_CHUNK_SIZE: usize = CHUNK_SIZE + 2;
55
pub const PADDED_CHUNK_USIZE: usize = PADDED_CHUNK_SIZE;
66
pub const CHUNK_LENGTH: usize = PADDED_CHUNK_SIZE * PADDED_CHUNK_SIZE * PADDED_CHUNK_SIZE;
77

8-
pub struct Chunk {
8+
#[derive(Clone)]
9+
pub struct Chunk {
910
pub data: [BlockId; CHUNK_LENGTH],
1011
pub position: Vec3,
1112
}

0 commit comments

Comments
 (0)