memory.rs
3.3 kB · rust · 116 lines
1use mrlycore::json::parse;2use mrlydemo::memory::*;3use mrlydemo::two::two_grid;45fn read(dimension: usize, width: usize, code: &str, levels: usize) -> mrlycore::Json {6 parse(&memory_read(dimension, width, code, levels).unwrap()).unwrap()7}89#[test]10fn width_one_is_the_plane_design_cell_for_cell() {11 for code in ["1", "7", "11", "13", "14", "9"] {12 for level in 1..=6 {13 let sheet = memory_sheet(2, 1, code, level).unwrap();14 let design = two_grid(code, 2, level, 0, 2).unwrap();15 assert_eq!(sheet.width, design.width, "code {code} level {level}");16 assert_eq!(sheet.height, design.height, "code {code} level {level}");17 assert_eq!(sheet.types, design.types, "code {code} level {level}");18 }19 }20}2122#[test]23fn the_golden_rule_counts_the_fibonacci_numbers() {24 let read = read(1, 2, "7", 8);25 assert_eq!(26 read["counts"].to_string(),27 "[2,3,5,8,13,21,34,55]".to_string()28 );29 assert_eq!(30 format!("{:.6}", read["perron"].as_f64().unwrap()),31 "1.618034"32 );33 assert_eq!(34 format!("{:.6}", read["exponent"].as_f64().unwrap()),35 "0.694242"36 );37 assert_eq!(38 format!("{:.6}", read["kappa"].as_f64().unwrap()),39 "0.098239"40 );41 assert_eq!(read["window_count"].as_u64().unwrap(), 3);42}4344#[test]45fn the_supergolden_rule_counts_the_narayana_cows() {46 let read = read(1, 3, "23", 8);47 assert_eq!(48 read["counts"].to_string(),49 "[2,4,4,6,9,13,19,28]".to_string()50 );51 assert_eq!(52 format!("{:.6}", read["perron"].as_f64().unwrap()),53 "1.465571"54 );55}5657#[test]58fn the_width_three_presets_name_the_plastic_and_tribonacci_roots() {59 let plastic = read(1, 3, "54", 8);60 assert_eq!(61 plastic["counts"].to_string(),62 "[2,4,4,5,7,9,12,16]".to_string()63 );64 assert_eq!(65 format!("{:.9}", plastic["perron"].as_f64().unwrap()),66 "1.324717957"67 );68 assert_eq!(69 format!("{:.6}", plastic["kappa"].as_f64().unwrap()),70 "0.260981"71 );72 let tribonacci = read(1, 3, "127", 8);73 assert_eq!(74 tribonacci["counts"].to_string(),75 "[2,4,7,13,24,44,81,149]".to_string()76 );77 assert_eq!(78 format!("{:.9}", tribonacci["perron"].as_f64().unwrap()),79 "1.839286755"80 );81 assert_eq!(82 format!("{:.6}", tribonacci["kappa"].as_f64().unwrap()),83 "0.056639"84 );85}8687#[test]88fn the_full_rule_carries_no_memory_cost() {89 let read = read(2, 2, "65535", 4);90 assert_eq!(read["counts"].to_string(), "[4,16,64,256]".to_string());91 assert_eq!(read["kappa"].as_f64().unwrap(), 0.0);92 assert_eq!(read["states"].as_u64().unwrap(), 4);93 assert_eq!(read["windows"].as_u64().unwrap(), 16);94}9596#[test]97fn the_cantor_sheet_is_one_row_a_level() {98 let sheet = memory_sheet(1, 2, "7", 6).unwrap();99 assert_eq!((sheet.width, sheet.height), (64, 6));100 let filled: Vec<u32> = (0..6)101 .map(|row| {102 sheet.types[row * 64..(row + 1) * 64]103 .iter()104 .map(|&b| b as u32)105 .sum()106 })107 .collect();108 assert_eq!(filled, vec![64, 48, 40, 32, 26, 21]);109}110111#[test]112fn a_rule_over_the_budget_is_refused() {113 assert!(memory_sheet(2, 1, "7", 9).is_err());114 assert!(memory_read(1, 7, "0", 4).is_err());115 assert!(memory_read(1, 2, "16", 4).is_err());116}