Skip to content

Commit

Permalink
Sorting example
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasgoergens committed Dec 13, 2024
1 parent 863f3ad commit 475e0c6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ceno_host/tests/test_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u32>()).collect::<Vec<_>>())?;

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<Vec<StepRecord>> {
let steps = state.iter_until_halt().collect::<Result<Vec<_>>>()?;
eprintln!("Emulator ran for {} steps.", steps.len());
Expand Down
1 change: 1 addition & 0 deletions examples-builder/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const EXAMPLES: &[&str] = &[
"ceno_rt_mini",
"ceno_rt_panic",
"hints",
"sorting",
];
const CARGO_MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");

Expand Down
16 changes: 16 additions & 0 deletions guest/examples/examples/sorting.rs
Original file line number Diff line number Diff line change
@@ -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<u32> = 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]);
}

0 comments on commit 475e0c6

Please sign in to comment.