diff --git a/ceno_host/tests/test_elf.rs b/ceno_host/tests/test_elf.rs index ceabe2413..799648d7b 100644 --- a/ceno_host/tests/test_elf.rs +++ b/ceno_host/tests/test_elf.rs @@ -93,6 +93,22 @@ fn test_hints() -> Result<()> { Ok(()) } +#[test] +fn test_sorting() -> Result<()> { + use rand::Rng; + let mut hints = CenoStdin::default(); + let mut rng = rand::thread_rng(); + + // Provide some random numbers to sort: + hints.write(&(0..1000).map(|_| rng.gen::()).collect::>())?; + + let all_messages = ceno_host::run(CENO_PLATFORM, ceno_examples::sorting, &hints); + for (i, msg) in enumerate(&all_messages) { + println!("{i}: {msg}"); + } + Ok(()) +} + fn run(state: &mut VMState) -> Result> { let steps = state.iter_until_halt().collect::>>()?; eprintln!("Emulator ran for {} steps.", steps.len()); diff --git a/examples-builder/build.rs b/examples-builder/build.rs index d11c176cf..1781fdfff 100644 --- a/examples-builder/build.rs +++ b/examples-builder/build.rs @@ -15,6 +15,7 @@ const EXAMPLES: &[&str] = &[ "ceno_rt_mini", "ceno_rt_panic", "hints", + "sorting", ]; const CARGO_MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR"); diff --git a/guest/examples/examples/sorting.rs b/guest/examples/examples/sorting.rs new file mode 100644 index 000000000..59b47c722 --- /dev/null +++ b/guest/examples/examples/sorting.rs @@ -0,0 +1,16 @@ +#![no_main] +#![no_std] + +extern crate ceno_rt; +use ceno_rt::println; +use core::fmt::Write; +use rkyv::vec::ArchivedVec; + +ceno_rt::entry!(main); +fn main() { + let input: &ArchivedVec = ceno_rt::read(); + let mut scratch = input.to_vec(); + scratch.sort(); + // Print any output you feel like, eg the first element of the sorted vector: + println!("{}", scratch[0]); +}