Adds more unit tests
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-06-11 20:51:37 +02:00
parent 0c076dd711
commit 56e1237c22
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
24 changed files with 306 additions and 42 deletions

View File

@ -1,2 +1,5 @@
pub mod growth_rate; pub mod growth_rate;
pub mod lookup_growth_rate; pub mod lookup_growth_rate;
pub use growth_rate::*;
pub use lookup_growth_rate::*;

View File

@ -1,2 +1,5 @@
pub mod item; pub mod item;
pub mod item_category; pub mod item_category;
pub use item::*;
pub use item_category::*;

View File

@ -1,5 +1,5 @@
use crate::static_data::libraries::data_library::DataLibrary; use crate::static_data::Ability;
use crate::static_data::species_data::ability::Ability; use crate::static_data::DataLibrary;
use crate::StringKey; use crate::StringKey;
use hashbrown::HashMap; use hashbrown::HashMap;
@ -53,4 +53,29 @@ pub mod tests {
lib lib
} }
#[test]
fn get_ability_library_access() {
let lib = build();
let ability = lib.get(&"test_ability".into());
assert!(ability.is_some());
assert_eq!(ability.unwrap().name(), &"test_ability".into());
}
#[test]
fn get_ability_library_direct_map_access() {
let lib = build();
let map = lib.map();
let ability = map.get(&"test_ability".into());
assert!(ability.is_some());
assert_eq!(ability.unwrap().name(), &"test_ability".into());
}
#[test]
fn get_ability_library_direct_list_access() {
let lib = build();
let list = lib.list_values();
assert_eq!(list.len(), 1);
assert!(list.contains(&StringKey::new("test_ability")));
}
} }

View File

@ -1,4 +1,4 @@
use crate::utils::random::Random; use crate::Random;
use crate::StringKey; use crate::StringKey;
use hashbrown::HashMap; use hashbrown::HashMap;

View File

@ -1,5 +1,5 @@
use crate::defines::LevelInt; use crate::defines::LevelInt;
use crate::static_data::growth_rates::growth_rate::GrowthRate; use crate::static_data::GrowthRate;
use crate::StringKey; use crate::StringKey;
use hashbrown::HashMap; use hashbrown::HashMap;
use std::fmt; use std::fmt;

View File

@ -1,5 +1,5 @@
use crate::static_data::items::item::Item; use crate::static_data::DataLibrary;
use crate::static_data::libraries::data_library::DataLibrary; use crate::static_data::Item;
use crate::StringKey; use crate::StringKey;
use hashbrown::HashMap; use hashbrown::HashMap;

View File

@ -7,3 +7,13 @@ pub mod move_library;
pub mod species_library; pub mod species_library;
pub mod static_data; pub mod static_data;
pub mod type_library; pub mod type_library;
pub use ability_library::AbilityLibrary;
pub use data_library::DataLibrary;
pub use growth_rate_library::GrowthRateLibrary;
pub use item_library::ItemLibrary;
pub use library_settings::LibrarySettings;
pub use move_library::MoveLibrary;
pub use species_library::SpeciesLibrary;
pub use static_data::StaticData;
pub use type_library::TypeLibrary;

View File

@ -1,5 +1,5 @@
use crate::static_data::libraries::data_library::DataLibrary; use crate::static_data::DataLibrary;
use crate::static_data::moves::move_data::MoveData; use crate::static_data::MoveData;
use crate::StringKey; use crate::StringKey;
use hashbrown::HashMap; use hashbrown::HashMap;

View File

@ -1,5 +1,5 @@
use crate::static_data::libraries::data_library::DataLibrary; use crate::static_data::DataLibrary;
use crate::static_data::species_data::species::Species; use crate::static_data::Species;
use crate::StringKey; use crate::StringKey;
use hashbrown::HashMap; use hashbrown::HashMap;

View File

@ -1,11 +1,11 @@
use crate::static_data::libraries::ability_library::AbilityLibrary; use crate::static_data::AbilityLibrary;
use crate::static_data::libraries::growth_rate_library::GrowthRateLibrary; use crate::static_data::GrowthRateLibrary;
use crate::static_data::libraries::item_library::ItemLibrary; use crate::static_data::ItemLibrary;
use crate::static_data::libraries::library_settings::LibrarySettings; use crate::static_data::LibrarySettings;
use crate::static_data::libraries::move_library::MoveLibrary; use crate::static_data::MoveLibrary;
use crate::static_data::libraries::species_library::SpeciesLibrary; use crate::static_data::NatureLibrary;
use crate::static_data::libraries::type_library::TypeLibrary; use crate::static_data::SpeciesLibrary;
use crate::static_data::natures::NatureLibrary; use crate::static_data::TypeLibrary;
#[derive(Debug)] #[derive(Debug)]
pub struct StaticData<'a> { pub struct StaticData<'a> {

View File

@ -6,3 +6,12 @@ pub mod natures;
pub mod species_data; pub mod species_data;
pub mod statistic_set; pub mod statistic_set;
pub mod statistics; pub mod statistics;
pub use growth_rates::*;
pub use items::*;
pub use libraries::*;
pub use moves::*;
pub use natures::*;
pub use species_data::*;
pub use statistic_set::*;
pub use statistics::*;

View File

@ -1,2 +1,9 @@
pub mod move_data; pub mod move_data;
pub mod secondary_effect; pub mod secondary_effect;
pub use move_data::MoveCategory;
pub use move_data::MoveData;
pub use move_data::MoveTarget;
pub use secondary_effect::EffectParameter;
pub use secondary_effect::SecondaryEffect;

View File

@ -1,4 +1,4 @@
use self::super::secondary_effect::SecondaryEffect; use crate::static_data::SecondaryEffect;
use crate::StringKey; use crate::StringKey;
use std::collections::HashSet; use std::collections::HashSet;
@ -10,7 +10,6 @@ pub enum MoveCategory {
} }
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
#[allow(dead_code)]
pub enum MoveTarget { pub enum MoveTarget {
Adjacent, Adjacent,
AdjacentAlly, AdjacentAlly,

View File

@ -52,12 +52,12 @@ mod tests {
#[test] #[test]
fn create_secondary_effect() { fn create_secondary_effect() {
let empty = SecondaryEffect::empty(); let empty = SecondaryEffect::empty();
assert_approx_eq!(empty.chance, 0.0); assert_approx_eq!(empty.chance(), 0.0);
assert_eq!(empty.effect_name, ""); assert_eq!(empty.effect_name(), "");
assert_eq!(empty.parameters.len(), 0); assert_eq!(empty.parameters().len(), 0);
let set = SecondaryEffect::new(50.0, "foo".to_string(), Vec::new()); let set = SecondaryEffect::new(50.0, "foo".to_string(), Vec::new());
assert_approx_eq!(set.chance, 50.0); assert_approx_eq!(set.chance(), 50.0);
assert_eq!(set.effect_name, "foo"); assert_eq!(set.effect_name(), "foo");
assert_eq!(set.parameters.len(), 0); assert_eq!(set.parameters().len(), 0);
} }
} }

View File

@ -1,4 +1,4 @@
use crate::static_data::statistics::Statistic; use crate::static_data::Statistic;
use crate::StringKey; use crate::StringKey;
use hashbrown::HashMap; use hashbrown::HashMap;

View File

@ -1,4 +1,4 @@
use crate::static_data::moves::secondary_effect::EffectParameter; use crate::static_data::EffectParameter;
use crate::StringKey; use crate::StringKey;
#[derive(Debug)] #[derive(Debug)]

View File

@ -1,9 +1,9 @@
use self::super::learnable_moves::LearnableMoves; use crate::static_data::Ability;
use crate::static_data::species_data::ability::Ability; use crate::static_data::AbilityIndex;
use crate::static_data::species_data::ability_index::AbilityIndex; use crate::static_data::LearnableMoves;
use crate::static_data::statistic_set::StatisticSet; use crate::static_data::Statistic;
use crate::static_data::statistics::Statistic; use crate::static_data::StatisticSet;
use crate::utils::random::Random; use crate::Random;
use crate::StringKey; use crate::StringKey;
use hashbrown::HashSet; use hashbrown::HashSet;
use std::ops::Deref; use std::ops::Deref;

View File

@ -1,5 +1,5 @@
use crate::defines::LevelInt; use crate::defines::LevelInt;
use crate::static_data::moves::move_data::MoveData; use crate::static_data::MoveData;
use hashbrown::hash_map::Entry::{Occupied, Vacant}; use hashbrown::hash_map::Entry::{Occupied, Vacant};
use hashbrown::HashMap; use hashbrown::HashMap;

View File

@ -1,6 +1,13 @@
pub mod ability;
pub mod ability_index; pub mod ability_index;
pub mod form; pub mod form;
pub mod gender; pub mod gender;
pub mod learnable_moves; pub mod learnable_moves;
pub mod species; pub mod species;
pub mod ability;
pub use ability::Ability;
pub use ability_index::AbilityIndex;
pub use form::Form;
pub use gender::Gender;
pub use learnable_moves::LearnableMoves;
pub use species::Species;

View File

@ -1,6 +1,6 @@
use self::super::form::Form; use crate::static_data::Form;
use crate::static_data::species_data::gender::Gender; use crate::static_data::Gender;
use crate::utils::random::Random; use crate::Random;
use crate::StringKey; use crate::StringKey;
use hashbrown::{HashMap, HashSet}; use hashbrown::{HashMap, HashSet};

View File

@ -1,5 +1,5 @@
use super::statistics::Statistic; use super::statistics::Statistic;
use num_traits::PrimInt; use num_traits::{cast, clamp, PrimInt};
#[derive(Default, Eq, PartialEq, Copy, Clone, Debug)] #[derive(Default, Eq, PartialEq, Copy, Clone, Debug)]
pub struct StatisticSet<T> pub struct StatisticSet<T>
@ -117,6 +117,25 @@ impl<T, const MIN: i64, const MAX: i64> ClampedStatisticSet<T, MIN, MAX>
where where
T: PrimInt, T: PrimInt,
{ {
pub fn new(
hp: T,
attack: T,
defense: T,
special_attack: T,
special_defense: T,
speed: T,
) -> Self {
Self {
hp: cast(clamp(cast::<T, i64>(hp).unwrap(), MIN, MAX)).unwrap(),
attack: cast(clamp(cast::<T, i64>(attack).unwrap(), MIN, MAX)).unwrap(),
defense: cast(clamp(cast::<T, i64>(defense).unwrap(), MIN, MAX)).unwrap(),
special_attack: cast(clamp(cast::<T, i64>(special_attack).unwrap(), MIN, MAX)).unwrap(),
special_defense: cast(clamp(cast::<T, i64>(special_defense).unwrap(), MIN, MAX))
.unwrap(),
speed: cast(clamp(cast::<T, i64>(speed).unwrap(), MIN, MAX)).unwrap(),
}
}
pub fn hp(&self) -> T { pub fn hp(&self) -> T {
self.hp self.hp
} }
@ -206,3 +225,105 @@ where
} }
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn create_get_values() {
let set = StatisticSet::new(1, 2, 3, 4, 5, 6);
assert_eq!(set.hp(), 1);
assert_eq!(set.get_stat(Statistic::HP), 1);
assert_eq!(set.attack(), 2);
assert_eq!(set.get_stat(Statistic::Attack), 2);
assert_eq!(set.defense(), 3);
assert_eq!(set.get_stat(Statistic::Defense), 3);
assert_eq!(set.special_attack(), 4);
assert_eq!(set.get_stat(Statistic::SpecialAttack), 4);
assert_eq!(set.special_defense(), 5);
assert_eq!(set.get_stat(Statistic::SpecialDefense), 5);
assert_eq!(set.speed(), 6);
assert_eq!(set.get_stat(Statistic::Speed), 6);
}
#[test]
fn create_set_value() {
let mut set = StatisticSet::new(1, 2, 3, 4, 5, 6);
set.set_stat(Statistic::HP, 20);
assert_eq!(set.hp(), 20);
set.set_stat(Statistic::Attack, 30);
assert_eq!(set.attack(), 30);
set.set_stat(Statistic::Defense, 40);
assert_eq!(set.defense(), 40);
set.set_stat(Statistic::SpecialAttack, 50);
assert_eq!(set.special_attack(), 50);
set.set_stat(Statistic::SpecialDefense, 60);
assert_eq!(set.special_defense(), 60);
set.set_stat(Statistic::Speed, 70);
assert_eq!(set.speed(), 70);
}
#[test]
fn create_increase_value() {
let mut set = StatisticSet::new(1, 2, 3, 4, 5, 6);
set.increase_stat(Statistic::SpecialAttack, 5);
assert_eq!(set.special_attack(), 9);
}
#[test]
fn create_decrease_value() {
let mut set = StatisticSet::new(1, 2, 3, 4, 5, 6);
set.decrease_stat(Statistic::SpecialAttack, 5);
assert_eq!(set.special_attack(), -1);
}
#[test]
fn create_clamped_get_values() {
let set = ClampedStatisticSet::<i32, { -2 }, 4>::new(-5, 2, 3, 4, 5, 6);
assert_eq!(set.hp(), -2);
assert_eq!(set.get_stat(Statistic::HP), -2);
assert_eq!(set.attack(), 2);
assert_eq!(set.get_stat(Statistic::Attack), 2);
assert_eq!(set.defense(), 3);
assert_eq!(set.get_stat(Statistic::Defense), 3);
assert_eq!(set.special_attack(), 4);
assert_eq!(set.get_stat(Statistic::SpecialAttack), 4);
assert_eq!(set.special_defense(), 4);
assert_eq!(set.get_stat(Statistic::SpecialDefense), 4);
assert_eq!(set.speed(), 4);
assert_eq!(set.get_stat(Statistic::Speed), 4);
}
#[test]
fn create_clamped_set_value() {
let mut set = ClampedStatisticSet::<i32, { -2 }, 4>::new(1, 2, 3, 4, 5, 6);
set.set_stat(Statistic::SpecialAttack, 20);
assert_eq!(set.special_attack(), 4);
set.set_stat(Statistic::SpecialAttack, -10);
assert_eq!(set.special_attack(), -2);
}
#[test]
fn create_clamped_increase_value() {
let mut set = ClampedStatisticSet::<i32, { -2 }, 4>::new(1, 2, 3, 2, 2, 6);
let mut has_changed = set.increase_stat(Statistic::SpecialAttack, 20);
assert!(has_changed);
assert_eq!(set.special_attack(), 4);
has_changed = set.increase_stat(Statistic::SpecialAttack, 2);
assert!(!has_changed);
assert_eq!(set.special_attack(), 4);
}
#[test]
fn create_clamped_decrease_value() {
let mut set = ClampedStatisticSet::<i32, { -2 }, 4>::new(1, 2, 3, 2, 2, 6);
let mut has_changed = set.decrease_stat(Statistic::SpecialAttack, 20);
assert!(has_changed);
assert_eq!(set.special_attack(), -2);
has_changed = set.decrease_stat(Statistic::SpecialAttack, 2);
assert!(!has_changed);
assert_eq!(set.special_attack(), -2);
}
}

View File

@ -1,4 +1,4 @@
#[derive(Debug, PartialEq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Statistic { pub enum Statistic {
HP, HP,
Attack, Attack,

View File

@ -80,7 +80,8 @@ mod tests {
#[cfg_attr(miri, ignore)] #[cfg_attr(miri, ignore)]
fn create_random() { fn create_random() {
let _default = Random::default(); let _default = Random::default();
let _empty = Random::new(100); let empty = Random::new(100);
assert_eq!(empty.get_seed(), 100);
} }
#[test] #[test]
@ -96,6 +97,19 @@ mod tests {
assert_eq!(v.get(), 580236580); assert_eq!(v.get(), 580236580);
} }
#[test]
fn get_random_unsigned_with_seed() {
let mut v = Random::new(10);
assert_eq!(v.get_unsigned(), 1755576946);
assert_eq!(v.get_unsigned(), 1254514019);
assert_eq!(v.get_unsigned(), 1735834837);
assert_eq!(v.get_unsigned(), 51079449);
assert_eq!(v.get_unsigned(), 506997516);
assert_eq!(v.get_unsigned(), 4121439675);
assert_eq!(v.get_unsigned(), 683138464);
assert_eq!(v.get_unsigned(), 580236580);
}
#[test] #[test]
fn get_random_with_limit_with_seed() { fn get_random_with_limit_with_seed() {
let mut v = Random::new(10); let mut v = Random::new(10);
@ -119,6 +133,12 @@ mod tests {
assert_eq!(v.get_max(2), 1); assert_eq!(v.get_max(2), 1);
} }
#[test]
fn get_random_unsigned_with_limit_with_seed() {
let mut v = Random::new(10);
assert_eq!(v.get_max_unsigned(4121439675), 1684647164);
}
#[test] #[test]
fn get_random_with_limit_with_range() { fn get_random_with_limit_with_range() {
let mut v = Random::new(10); let mut v = Random::new(10);
@ -132,6 +152,33 @@ mod tests {
assert_eq!(v.get_between(10, 30), 12); assert_eq!(v.get_between(10, 30), 12);
} }
#[test]
fn get_random_unsigned_with_limit_with_range() {
let mut v = Random::new(10);
assert_eq!(v.get_between_unsigned(4121439675, 4121439678), 4121439676);
}
#[test]
fn get_random_float() {
let mut v = Random::new(10);
assert_eq!(v.get_float(), 0.2920893);
}
#[test]
fn get_random_double() {
let mut v = Random::new(10);
assert_eq!(v.get_double(), 0.2920893066117427);
}
#[test]
fn get_random_clone_then_continue() {
let mut v = Random::new(10);
assert_eq!(v.get_unsigned(), 1755576946);
let mut clone = v.clone();
assert_eq!(clone.get_unsigned(), 1254514019);
assert_eq!(v.get_unsigned(), 1254514019);
}
#[test] #[test]
#[cfg_attr(miri, ignore)] #[cfg_attr(miri, ignore)]
fn check_random_distribution() { fn check_random_distribution() {

View File

@ -109,3 +109,36 @@ const CRC_TABLE: &[u32] = &[
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,
]; ];
#[cfg(test)]
mod tests {
extern crate test;
use crate::StringKey;
#[test]
fn create_empty_stringkey() {
let sk = StringKey::new("");
assert_eq!(sk.str(), "");
assert_eq!(sk.hash(), 0);
assert_eq!(sk.hash(), StringKey::get_hash_const(b""));
}
#[test]
fn create_stringkey_foo() {
let sk = StringKey::new("foo");
assert_eq!(sk.str(), "foo");
assert_eq!(sk.hash(), 2356372769);
assert_eq!(sk.hash(), StringKey::get_hash_const(b"foo"));
assert_eq!(sk.hash(), StringKey::get_hash_const(b"FOo"));
}
#[test]
fn create_stringkey_bar() {
let sk = StringKey::new("bar");
assert_eq!(sk.str(), "bar");
assert_eq!(sk.hash(), 1996459178);
assert_eq!(sk.hash(), StringKey::get_hash_const(b"bar"));
assert_eq!(sk.hash(), StringKey::get_hash_const(b"baR"));
}
}