Skip to content

Commit

Permalink
tests: add test for repetaer t-flip-flop
Browse files Browse the repository at this point in the history
  • Loading branch information
StackDoubleFlow committed Jan 19, 2025
1 parent 10d5953 commit e63c7b1
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 57 deletions.
74 changes: 71 additions & 3 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use mchprs_blocks::block_entities::BlockEntity;
use mchprs_blocks::blocks::Block;
use mchprs_blocks::BlockPos;
use mchprs_blocks::blocks::{Block, Lever, LeverFace, RedstoneRepeater};
use mchprs_blocks::{BlockDirection, BlockPos};
use mchprs_redpiler::{BackendVariant, Compiler, CompilerOptions};
use mchprs_redstone::wire::make_cross;
use mchprs_world::storage::Chunk;
use mchprs_world::{TickEntry, TickPriority, World};

Expand All @@ -13,6 +14,7 @@ pub struct TestWorld {
}

impl TestWorld {
/// Create a new square world for testing with a size in chunks
pub fn new(size: i32) -> TestWorld {
let mut chunks = Vec::new();
for x in 0..size {
Expand Down Expand Up @@ -243,8 +245,74 @@ macro_rules! test_all_backends {
#[test]
fn [< $name _redstone >]() { $name(TestBackend::Redstone) }
#[test]
fn [< $name _rp_direct >]() { $name(TestBackend::Redpiler(BackendVariant::Direct)) }
fn [< $name _rp_direct >]() { $name(TestBackend::Redpiler(::mchprs_redpiler::BackendVariant::Direct)) }
}
};
}
pub(crate) use test_all_backends;

/// Helper function to create a BlockPos
pub fn pos(x: i32, y: i32, z: i32) -> BlockPos {
BlockPos::new(x, y, z)
}

/// Place a block with a block of sandstone below it
pub fn place_on_block(world: &mut TestWorld, block_pos: BlockPos, block: Block) {
world.set_block(block_pos - pos(0, 1, 0), Block::Sandstone {});
world.set_block(block_pos, block);
}

pub fn trapdoor() -> Block {
Block::IronTrapdoor {
facing: Default::default(),
half: Default::default(),
powered: false,
}
}

/// Creates a lever at `lever_pos` with a block of sandstone below it
pub fn make_lever(world: &mut TestWorld, lever_pos: BlockPos) {
place_on_block(
world,
lever_pos,
Block::Lever {
lever: Lever {
face: LeverFace::Floor,
..Default::default()
},
},
);
}

/// Creates a repeater at `repeater_pos` with a block of sandstone below it
pub fn make_repeater(
world: &mut TestWorld,
repeater_pos: BlockPos,
delay: u8,
direction: BlockDirection,
) {
place_on_block(
world,
repeater_pos,
Block::RedstoneRepeater {
repeater: RedstoneRepeater {
delay,
facing: direction,
locked: false,
powered: false,
..Default::default()
},
},
);
}

/// Creates a wire at `wire_pos` with a block of sandstone below it
pub fn make_wire(world: &mut TestWorld, wire_pos: BlockPos) {
place_on_block(
world,
wire_pos,
Block::RedstoneWire {
wire: make_cross(0),
},
);
}
59 changes: 5 additions & 54 deletions tests/components.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,10 @@
mod common;
use common::*;

use common::{test_all_backends, BackendRunner, TestBackend, TestWorld};
use mchprs_blocks::blocks::{Block, Lever, LeverFace, RedstoneRepeater};
use mchprs_blocks::{BlockDirection, BlockPos};
use mchprs_redpiler::BackendVariant;
use mchprs_redstone::wire::make_cross;
use mchprs_blocks::blocks::Block;
use mchprs_blocks::BlockDirection;
use mchprs_world::World;

fn pos(x: i32, y: i32, z: i32) -> BlockPos {
BlockPos::new(x, y, z)
}

fn place_on_block(world: &mut TestWorld, block_pos: BlockPos, block: Block) {
world.set_block(block_pos - pos(0, 1, 0), Block::Sandstone {});
world.set_block(block_pos, block);
}

fn trapdoor() -> Block {
Block::IronTrapdoor {
facing: Default::default(),
half: Default::default(),
powered: false,
}
}

/// Creates a lever at `lever_pos` with a block of sandstone below it
fn make_lever(world: &mut TestWorld, lever_pos: BlockPos) {
place_on_block(
world,
lever_pos,
Block::Lever {
lever: Lever {
face: LeverFace::Floor,
..Default::default()
},
},
);
}

test_all_backends!(lever_on_off);
fn lever_on_off(backend: TestBackend) {
let lever_pos = pos(0, 1, 0);
Expand Down Expand Up @@ -128,13 +95,7 @@ fn torch_on_off(backend: TestBackend) {

let mut world = TestWorld::new(1);
make_lever(&mut world, lever_pos);
place_on_block(
&mut world,
pos(1, 1, 0),
Block::RedstoneWire {
wire: make_cross(0),
},
);
make_wire(&mut world, pos(1, 1, 0));
place_on_block(&mut world, torch_pos, Block::RedstoneTorch { lit: true });

let mut runner = BackendRunner::new(world, backend);
Expand All @@ -157,17 +118,7 @@ fn repeater_on_off(backend: TestBackend) {
for delay in 1..=4 {
let mut world = TestWorld::new(1);
make_lever(&mut world, lever_pos);
place_on_block(
&mut world,
pos(1, 1, 0),
Block::RedstoneRepeater {
repeater: RedstoneRepeater {
facing: BlockDirection::West,
delay: delay as u8,
..Default::default()
},
},
);
make_repeater(&mut world, pos(1, 1, 0), delay as u8, BlockDirection::West);
world.set_block(trapdoor_pos, trapdoor());

let mut runner = BackendRunner::new(world, backend);
Expand Down
44 changes: 44 additions & 0 deletions tests/timings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
mod common;
use common::*;
use mchprs_blocks::BlockDirection;

test_all_backends!(repeater_t_flip_flop);
fn repeater_t_flip_flop(backend: TestBackend) {
// RS -> Repeater South
// Layout:
// W RN W
// W RN RE
// L

let mut world = TestWorld::new(1);

let output_pos = pos(1, 1, 2);
let lever_pos = pos(0, 1, 0);

make_lever(&mut world, lever_pos);
make_wire(&mut world, pos(1, 1, 0));
make_wire(&mut world, pos(2, 1, 0));

make_repeater(&mut world, pos(1, 1, 1), 1, BlockDirection::North);
make_repeater(&mut world, pos(2, 1, 1), 1, BlockDirection::North);

make_repeater(&mut world, output_pos, 1, BlockDirection::East);
make_wire(&mut world, pos(2, 1, 2));

let mut runner = BackendRunner::new(world, backend);
// Set up initial state
runner.use_block(lever_pos);
runner.check_powered_for(output_pos, false, 2);

// Toggle flip flop on
runner.use_block(lever_pos);
runner.check_powered_for(output_pos, false, 2);
runner.use_block(lever_pos);
runner.check_powered_for(output_pos, true, 10);

// Toggle flip flop off
runner.use_block(lever_pos);
runner.check_powered_for(output_pos, true, 2);
runner.use_block(lever_pos);
runner.check_powered_for(output_pos, false, 10);
}

0 comments on commit e63c7b1

Please sign in to comment.