mirror of
https://github.com/tweag/nickel.git
synced 2024-10-07 00:30:47 +03:00
Use proper pretty printing when generating in ncl_random_array
This commit is contained in:
parent
6d6abf78f0
commit
96cdad988c
@ -1,6 +1,8 @@
|
|||||||
use criterion::{criterion_main, Criterion};
|
use criterion::{criterion_main, Criterion};
|
||||||
|
use nickel_lang::term::{ArrayAttrs, RichTerm, Term};
|
||||||
use nickel_lang_utilities::{ncl_bench_group, EvalMode};
|
use nickel_lang_utilities::{ncl_bench_group, EvalMode};
|
||||||
use pprof::criterion::{Output, PProfProfiler};
|
use pprof::criterion::{Output, PProfProfiler};
|
||||||
|
use pretty::{BoxAllocator, DocBuilder, Pretty};
|
||||||
|
|
||||||
/// Generates a pseaudo-random Nickel array as a string.
|
/// Generates a pseaudo-random Nickel array as a string.
|
||||||
fn ncl_random_array(len: usize) -> String {
|
fn ncl_random_array(len: usize) -> String {
|
||||||
@ -9,15 +11,18 @@ fn ncl_random_array(len: usize) -> String {
|
|||||||
let c = 1013904223;
|
let c = 1013904223;
|
||||||
|
|
||||||
let mut numbers = Vec::with_capacity(len);
|
let mut numbers = Vec::with_capacity(len);
|
||||||
numbers.push(1337);
|
let mut acc = 1337;
|
||||||
|
|
||||||
for _ in 0..len {
|
for _ in 0..len {
|
||||||
let x = *numbers.last().unwrap();
|
acc = (a * acc + c) % m;
|
||||||
numbers.push((a * x + c) % m);
|
numbers.push(RichTerm::from(Term::Num(acc as f64)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// HACK: It so happens that this is valid Nickel syntax.
|
let xs = RichTerm::from(Term::Array(numbers, ArrayAttrs::default()));
|
||||||
format!("{:?}", numbers)
|
let doc: DocBuilder<_, ()> = xs.pretty(&BoxAllocator);
|
||||||
|
let mut out = Vec::new();
|
||||||
|
doc.render(80, &mut out).unwrap();
|
||||||
|
String::from_utf8(out).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
ncl_bench_group! {
|
ncl_bench_group! {
|
||||||
|
Loading…
Reference in New Issue
Block a user