Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test utils #738

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ de_pathing = { path = "crates/pathing", version = "0.1.0-dev" }
de_signs = { path = "crates/signs", version = "0.1.0-dev" }
de_spawner = { path = "crates/spawner", version = "0.1.0-dev" }
de_terrain = { path = "crates/terrain", version = "0.1.0-dev" }
de_test_utils = { path = "crates/test_utils", version = "0.1.0-dev" }
de_types = { path = "crates/types", version = "0.1.0-dev" }
de_uom = { path = "crates/uom", version = "0.1.0-dev" }

Expand Down
4 changes: 4 additions & 0 deletions crates/index/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ ahash.workspace = true
nalgebra.workspace = true

[dev-dependencies]
# DE
de_test_utils.workspace = true

# Other
criterion.workspace = true

[[bench]]
Expand Down
30 changes: 4 additions & 26 deletions crates/index/benches/ray.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
use std::{
fs::File,
io::{BufRead, BufReader},
path::PathBuf,
};

use bevy::prelude::*;
use criterion::{
criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration,
Throughput,
};
use de_index::{EntityIndex, LocalCollider, SpatialQuery};
use de_objects::ObjectCollider;
use de_test_utils::load_points;
use glam::Vec2;
use parry3d::{
math::{Isometry, Point, Vector},
Expand Down Expand Up @@ -57,25 +52,8 @@ impl Iterator for Rays {
}
}

fn load_points(number: u32) -> Vec<Vec2> {
let mut points_path: PathBuf = env!("CARGO_MANIFEST_DIR").into();
points_path.push("test_data");
points_path.push(format!("{number}-points.txt"));
let reader = BufReader::new(File::open(points_path).unwrap());

let mut points = Vec::with_capacity(number as usize);
for line in reader.lines() {
let line = line.unwrap();
let mut numbers = line.split_whitespace();
let x: f32 = numbers.next().unwrap().parse().unwrap();
let y: f32 = numbers.next().unwrap().parse().unwrap();
points.push(MAP_SIZE * Vec2::new(x, y));
}
points
}

fn setup_world(world: &mut World, num_entities: u32, max_distance: f32) {
let points = load_points(num_entities);
let points = load_points(&num_entities.try_into().unwrap(), MAP_SIZE);
let mut index = EntityIndex::new();

for (i, point) in points.iter().enumerate() {
Expand All @@ -87,8 +65,8 @@ fn setup_world(world: &mut World, num_entities: u32, max_distance: f32) {
}

let mut rays = Rays::new();
let ray_origins = load_points(1000);
let ray_dirs = load_points(1000);
let ray_origins = load_points(&1000.try_into().unwrap(), MAP_SIZE);
JackCrumpLeys marked this conversation as resolved.
Show resolved Hide resolved
let ray_dirs = load_points(&1000.try_into().unwrap(), MAP_SIZE);
for (origin, dir) in ray_origins.iter().zip(ray_dirs.iter()) {
let dir = if dir.length() < 0.0001 {
Vec2::new(1., 0.)
Expand Down
17 changes: 17 additions & 0 deletions crates/test_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "de_test_utils"
description = "Test utilities for Digital Extinction"

version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
keywords.workspace = true
homepage.workspace = true
license.workspace = true
categories.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
glam.workspace = true
89 changes: 89 additions & 0 deletions crates/test_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;

use glam::Vec2;

/// An enum to allow for safe selection of the number of points to load from the test data.
#[derive(Copy, Clone, Debug)]
pub enum NumPoints {
OneHundred,
OneThousand,
TenThousand,
OneHundredThousand,
}

impl TryFrom<u32> for NumPoints {
type Error = &'static str;

fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
100 => Ok(Self::OneHundred),
1000 => Ok(Self::OneThousand),
10_000 => Ok(Self::TenThousand),
100_000 => Ok(Self::OneHundredThousand),
_ => Err("Invalid number of points"),
}
}
}

impl From<NumPoints> for usize {
fn from(value: NumPoints) -> Self {
match value {
NumPoints::OneHundred => 100,
JackCrumpLeys marked this conversation as resolved.
Show resolved Hide resolved
NumPoints::OneThousand => 1000,
NumPoints::TenThousand => 10_000,
NumPoints::OneHundredThousand => 100_000,
}
}
}

impl From<&NumPoints> for usize {
fn from(value: &NumPoints) -> Self {
match value {
NumPoints::OneHundred => 100,
NumPoints::OneThousand => 1000,
NumPoints::TenThousand => 10_000,
NumPoints::OneHundredThousand => 100_000,
}
}
}

/// Load deterministic points for testing.
///
/// # Arguments
/// * `number` - the selected number of points from the [NumPoints] enum.
/// * `max_value` - the max and min value for the returned point, the numbers returned will be
/// between -max_value and +max_value.
///
/// # Returns
/// A list of Vec2 points with x and y between -max_value and +max_value. This is guaranteed to be
/// deterministic across calls with the same input.
pub fn load_points(number: &NumPoints, max_value: f32) -> Vec<Vec2> {
let number: usize = number.into();

let mut points_path: PathBuf = env!("CARGO_MANIFEST_DIR").into();
points_path.push("test_data");
points_path.push(format!("{number}-points.txt"));
let reader = BufReader::new(File::open(points_path).unwrap());

let mut points = Vec::with_capacity(number);
for line in reader.lines() {
let line = line.unwrap();
let mut numbers = line.split_whitespace();
let x: f32 = numbers.next().unwrap().parse().unwrap();

assert!(x.is_finite());
assert!(x >= 0.);
assert!(x <= 1.);

let y: f32 = numbers.next().unwrap().parse().unwrap();

assert!(y.is_finite());
assert!(y >= 0.);
assert!(y <= 1.);

points.push(max_value * 2. * (Vec2::new(x, y) - 0.5));
}
points
}