apollonian.rs

4.1 kB · rust · 123 lines

1use mrlycore::json::parse;2use mrlycore::Json;3use mrlydemo::apollonian::*;45fn read(root: &str, cap: u32, order: usize) -> Json {6    parse(&apollonian_read(root, cap, order).unwrap()).unwrap()7}89#[test]10fn the_growth_lands_on_the_counts_the_generator_prints() {11    let strip = read("strip", 2048, 32);12    assert_eq!(13        format!(14            "{} {} {} {} {}",15            strip["circles"], strip["drawn"], strip["quads"], strip["broken"], strip["strayed"]16        ),17        "2448 2450 2449 0 0"18    );19    assert_eq!(read("strip", 1000, 32)["circles"], 950);20    assert_eq!(read("-1,2,2,3", 1000, 32)["circles"], 3325);21    assert_eq!(read("-2,3,6,7", 1000, 32)["circles"], 1297);22    assert_eq!(read("-3,4,12,13", 1000, 32)["circles"], 741);23    assert_eq!(24        format!("{:.6}", strip["exponent"].as_f64().unwrap()),25        "1.292481"26    );27    assert_eq!(strip["from"], 512);28}2930#[test]31fn every_circle_comes_back_as_a_centre_a_radius_and_its_curvature() {32    let circles = apollonian("strip", 2048).unwrap();33    assert_eq!(circles.len(), 2448 * 5);34    assert_eq!(circles[..5], [0.5, 0.125, 0.125, 8.0, 2.0]);35    for block in circles.chunks(5) {36        let [x, y, r, k, den] = [block[0], block[1], block[2], block[3], block[4]];37        assert!((r * k.abs() - 1.0).abs() < 1e-12);38        assert!(x > -r && x < 1.0 + r && y > 0.0 && y < 1.0);39        if den > 0.0 {40            assert!((y - r).abs() < 1e-12 && (k - 2.0 * den * den).abs() < 1e-12);41        }42    }43    assert_eq!(44        apollonian_root("strip").unwrap(),45        [0.0, 0.5, 0.5, 2.0, 1.0, 1.0, 0.5, 0.5, 2.0, 1.0]46    );47    let bounded = read("-1,2,2,3", 1000, 32);48    assert_eq!(apollonian_root("-1,2,2,3").unwrap().len(), 20);49    assert_eq!(bounded["frame"].to_string(), "[-1.0,-1.0,1.0,1.0]");50    assert_eq!(51        read("strip", 2048, 32)["frame"].to_string(),52        "[0.0,0.0,1.0,1.0]"53    );54}5556#[test]57fn the_ford_circles_rest_on_the_reduced_fractions() {58    let strip = read("strip", 2048, 32);59    assert_eq!(format!("{} {}", strip["line"], strip["ford"]), "323 323");60    let marks = apollonian_touches("strip", 2048).unwrap();61    assert_eq!(marks.len(), 323 * 4);62    assert_eq!(marks[..4], [0.03125, 1.0, 32.0, 2048.0]);63    assert_eq!(marks[marks.len() - 4..], [0.96875, 31.0, 32.0, 2048.0]);64    let mut last = 0.0;65    for block in marks.chunks(4) {66        let [at, num, den, k] = [block[0], block[1], block[2], block[3]];67        assert!(at > last && (at - num / den).abs() < 1e-12);68        assert_eq!(k, 2.0 * den * den);69        last = at;70    }71    assert!(apollonian_touches("-1,2,2,3", 2048).unwrap().is_empty());72    assert_eq!(read("-1,2,2,3", 1000, 32)["line"], 0);73}7475#[test]76fn the_stack_is_the_shadow_and_the_brightness_closes() {77    let deep = read("strip", 2048, 32);78    let shadow = &deep["shadow"];79    assert_eq!(80        format!(81            "{} {} {} {} {} {} {} {}",82            shadow["reach"],83            shadow["covered"],84            shadow["nodes"],85            shadow["touched"],86            shadow["missed"],87            shadow["offford"],88            shadow["bright"],89            shadow["want"]90        ),91        "2048 true 323 323 0 0 528 528"92    );93    let shallow = read("strip", 2048, 16);94    assert_eq!(95        format!(96            "{} {} {} {}",97            shallow["shadow"]["nodes"],98            shallow["shadow"]["touched"],99            shallow["shadow"]["missed"],100            shallow["shadow"]["bright"]101        ),102        "79 79 0 136"103    );104    assert_eq!(read("strip", 512, 32)["shadow"]["covered"], false);105    assert_eq!(read("-1,2,2,3", 1000, 32)["shadow"]["nodes"], 0);106}107108#[test]109fn a_root_off_the_list_or_a_cap_past_the_ceiling_is_refused() {110    let caps = parse(&apollonian_caps()).unwrap();111    assert_eq!(112        format!(113            "{} {} {}",114            caps["curvature"], caps["circles"], caps["order"]115        ),116        "8192 200000 64"117    );118    assert_eq!(caps["roots"].as_array().unwrap().len(), 4);119    assert!(apollonian("gasket", 2048).is_err());120    assert!(apollonian("strip", 8193).is_err());121    assert!(apollonian("strip", 1).is_err());122    assert!(apollonian_read("strip", 2048, 65).is_err());123}