rustlings/src/app_state.rs

649 lines
22 KiB
Rust
Raw Normal View History

2024-10-10 13:43:35 -04:00
use anyhow::{bail, Context, Error, Result};
2024-10-13 17:28:17 -04:00
use crossterm::{cursor, terminal, QueueableCommand};
2024-04-13 23:13:27 -04:00
use std::{
env,
fs::{File, OpenOptions},
2024-10-10 13:43:35 -04:00
io::{Read, Seek, StdoutLock, Write},
2024-09-03 20:19:45 -04:00
path::{Path, MAIN_SEPARATOR_STR},
2024-04-18 06:41:17 -04:00
process::{Command, Stdio},
2024-10-10 13:43:35 -04:00
sync::{
atomic::{AtomicUsize, Ordering::Relaxed},
mpsc,
},
2024-07-28 11:39:46 -04:00
thread,
2024-04-13 23:13:27 -04:00
};
2024-04-24 19:56:01 -04:00
use crate::{
2024-04-29 19:41:08 -04:00
clear_terminal,
2024-08-01 09:23:54 -04:00
cmd::CmdRunner,
2024-08-08 17:46:21 -04:00
collections::hash_set_with_capacity,
2024-04-24 19:56:01 -04:00
embedded::EMBEDDED_FILES,
2024-07-28 14:30:23 -04:00
exercise::{Exercise, RunnableExercise},
2024-04-24 19:56:01 -04:00
info_file::ExerciseInfo,
2024-10-13 19:28:12 -04:00
term::{self, CheckProgressVisualizer},
2024-04-24 19:56:01 -04:00
};
2024-04-13 23:13:27 -04:00
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
const DEFAULT_CHECK_PARALLELISM: usize = 8;
2024-04-12 14:06:56 -04:00
#[must_use]
pub enum ExercisesProgress {
2024-05-12 20:32:25 -04:00
// All exercises are done.
2024-04-12 14:06:56 -04:00
AllDone,
2024-05-12 20:32:25 -04:00
// A new exercise is now pending.
NewPending,
2024-09-05 11:32:59 -04:00
// The current exercise is still pending.
CurrentPending,
2024-04-12 14:06:56 -04:00
}
2024-04-14 10:03:49 -04:00
pub enum StateFileStatus {
Read,
NotRead,
}
2024-10-10 13:43:35 -04:00
#[derive(Clone, Copy)]
2024-10-13 19:28:12 -04:00
pub enum CheckProgress {
2024-10-13 17:28:17 -04:00
None,
Checking,
2024-10-10 13:43:35 -04:00
Done,
Pending,
}
pub struct AppState {
2024-04-13 19:15:43 -04:00
current_exercise_ind: usize,
exercises: Vec<Exercise>,
2024-04-29 11:01:47 -04:00
// Caches the number of done exercises to avoid iterating over all exercises every time.
n_done: u16,
2024-04-13 19:15:43 -04:00
final_message: String,
state_file: File,
2024-04-29 11:01:47 -04:00
// Preallocated buffer for reading and writing the state file.
2024-04-13 23:13:27 -04:00
file_buf: Vec<u8>,
2024-04-18 06:41:17 -04:00
official_exercises: bool,
2024-08-01 09:23:54 -04:00
cmd_runner: CmdRunner,
// Running in VS Code.
vs_code: bool,
}
impl AppState {
2024-04-14 10:03:49 -04:00
pub fn new(
exercise_infos: Vec<ExerciseInfo>,
final_message: String,
2024-04-27 11:31:51 -04:00
) -> Result<(Self, StateFileStatus)> {
2024-08-01 09:23:54 -04:00
let cmd_runner = CmdRunner::build()?;
let mut state_file = OpenOptions::new()
.create(true)
.read(true)
.write(true)
.truncate(false)
.open(STATE_FILE_NAME)
.with_context(|| {
format!("Failed to open or create the state file {STATE_FILE_NAME}")
})?;
2024-09-03 20:19:45 -04:00
let dir_canonical_path = term::canonicalize("exercises");
let mut exercises = exercise_infos
2024-04-13 19:15:43 -04:00
.into_iter()
2024-05-13 15:36:20 -04:00
.map(|exercise_info| {
// Leaking to be able to borrow in the watch mode `Table`.
// Leaking is not a problem because the `AppState` instance lives until
// the end of the program.
let path = exercise_info.path().leak();
let name = exercise_info.name.leak();
let dir = exercise_info.dir.map(|dir| &*dir.leak());
2024-08-02 10:28:05 -04:00
let hint = exercise_info.hint.leak().trim_ascii();
2024-05-13 15:36:20 -04:00
2024-09-03 20:19:45 -04:00
let canonical_path = dir_canonical_path.as_deref().map(|dir_canonical_path| {
let mut canonical_path;
if let Some(dir) = dir {
canonical_path = String::with_capacity(
2 + dir_canonical_path.len() + dir.len() + name.len(),
);
canonical_path.push_str(dir_canonical_path);
canonical_path.push_str(MAIN_SEPARATOR_STR);
canonical_path.push_str(dir);
} else {
canonical_path =
String::with_capacity(1 + dir_canonical_path.len() + name.len());
canonical_path.push_str(dir_canonical_path);
}
canonical_path.push_str(MAIN_SEPARATOR_STR);
canonical_path.push_str(name);
canonical_path.push_str(".rs");
canonical_path
});
2024-05-13 15:36:20 -04:00
Exercise {
dir,
name,
path,
2024-09-03 20:19:45 -04:00
canonical_path,
2024-05-13 15:36:20 -04:00
test: exercise_info.test,
strict_clippy: exercise_info.strict_clippy,
hint,
// Updated below.
2024-05-13 15:36:20 -04:00
done: false,
}
})
2024-04-13 19:15:43 -04:00
.collect::<Vec<_>>();
let mut current_exercise_ind = 0;
let mut n_done = 0;
let mut file_buf = Vec::with_capacity(2048);
let state_file_status = 'block: {
if state_file.read_to_end(&mut file_buf).is_err() {
break 'block StateFileStatus::NotRead;
}
// See `Self::write` for more information about the file format.
let mut lines = file_buf.split(|c| *c == b'\n').skip(2);
let Some(current_exercise_name) = lines.next() else {
break 'block StateFileStatus::NotRead;
};
if current_exercise_name.is_empty() || lines.next().is_none() {
break 'block StateFileStatus::NotRead;
}
let mut done_exercises = hash_set_with_capacity(exercises.len());
for done_exerise_name in lines {
if done_exerise_name.is_empty() {
break;
}
done_exercises.insert(done_exerise_name);
}
for (ind, exercise) in exercises.iter_mut().enumerate() {
if done_exercises.contains(exercise.name.as_bytes()) {
exercise.done = true;
n_done += 1;
}
if exercise.name.as_bytes() == current_exercise_name {
current_exercise_ind = ind;
}
}
StateFileStatus::Read
};
file_buf.clear();
file_buf.extend_from_slice(STATE_FILE_HEADER);
let slf = Self {
current_exercise_ind,
exercises,
n_done,
2024-04-14 10:03:49 -04:00
final_message,
state_file,
file_buf,
2024-04-18 06:41:17 -04:00
official_exercises: !Path::new("info.toml").exists(),
2024-08-01 09:23:54 -04:00
cmd_runner,
vs_code: env::var_os("TERM_PROGRAM").is_some_and(|v| v == "vscode"),
2024-04-13 23:13:27 -04:00
};
2024-04-27 11:31:51 -04:00
Ok((slf, state_file_status))
}
#[inline]
pub fn current_exercise_ind(&self) -> usize {
2024-04-13 19:15:43 -04:00
self.current_exercise_ind
}
#[inline]
2024-04-13 19:15:43 -04:00
pub fn exercises(&self) -> &[Exercise] {
&self.exercises
}
#[inline]
pub fn n_done(&self) -> u16 {
self.n_done
}
2024-10-13 16:02:41 -04:00
#[inline]
pub fn n_pending(&self) -> u16 {
self.exercises.len() as u16 - self.n_done
}
#[inline]
2024-04-13 19:15:43 -04:00
pub fn current_exercise(&self) -> &Exercise {
&self.exercises[self.current_exercise_ind]
}
#[inline]
2024-08-01 09:23:54 -04:00
pub fn cmd_runner(&self) -> &CmdRunner {
&self.cmd_runner
}
#[inline]
pub fn vs_code(&self) -> bool {
self.vs_code
}
2024-04-29 11:01:47 -04:00
// Write the state file.
// The file's format is very simple:
// - The first line is a comment.
// - The second line is an empty line.
// - The third line is the name of the current exercise. It must end with `\n` even if there
// are no done exercises.
// - The fourth line is an empty line.
// - All remaining lines are the names of done exercises.
fn write(&mut self) -> Result<()> {
self.file_buf.truncate(STATE_FILE_HEADER.len());
2024-04-29 11:01:47 -04:00
self.file_buf
.extend_from_slice(self.current_exercise().name.as_bytes());
self.file_buf.push(b'\n');
for exercise in &self.exercises {
if exercise.done {
self.file_buf.push(b'\n');
self.file_buf.extend_from_slice(exercise.name.as_bytes());
}
}
self.state_file
.rewind()
.with_context(|| format!("Failed to rewind the state file {STATE_FILE_NAME}"))?;
self.state_file
.set_len(0)
.with_context(|| format!("Failed to truncate the state file {STATE_FILE_NAME}"))?;
self.state_file
.write_all(&self.file_buf)
2024-04-29 11:01:47 -04:00
.with_context(|| format!("Failed to write the state file {STATE_FILE_NAME}"))?;
Ok(())
}
2024-05-12 11:40:53 -04:00
pub fn set_current_exercise_ind(&mut self, exercise_ind: usize) -> Result<()> {
2024-05-13 11:06:11 -04:00
if exercise_ind == self.current_exercise_ind {
return Ok(());
}
2024-05-12 11:40:53 -04:00
if exercise_ind >= self.exercises.len() {
bail!(BAD_INDEX_ERR);
}
2024-05-12 11:40:53 -04:00
self.current_exercise_ind = exercise_ind;
2024-04-13 23:13:27 -04:00
self.write()
}
pub fn set_current_exercise_by_name(&mut self, name: &str) -> Result<()> {
2024-04-13 19:15:43 -04:00
// O(N) is fine since this method is used only once until the program exits.
// Building a hashmap would have more overhead.
self.current_exercise_ind = self
.exercises
.iter()
2024-04-13 19:15:43 -04:00
.position(|exercise| exercise.name == name)
.with_context(|| format!("No exercise found for '{name}'!"))?;
2024-04-13 23:13:27 -04:00
self.write()
}
// Set the status of an exercise without saving. Returns `true` if the
2024-10-10 13:43:35 -04:00
// status actually changed (and thus needs saving later).
pub fn set_status(&mut self, exercise_ind: usize, done: bool) -> Result<bool> {
2024-05-12 11:40:53 -04:00
let exercise = self
.exercises
.get_mut(exercise_ind)
.context(BAD_INDEX_ERR)?;
2024-04-13 19:15:43 -04:00
if exercise.done == done {
2024-10-10 13:43:35 -04:00
return Ok(false);
}
exercise.done = done;
if done {
self.n_done += 1;
} else {
2024-10-10 13:43:35 -04:00
self.n_done -= 1;
}
2024-10-10 13:43:35 -04:00
Ok(true)
}
2024-10-10 13:43:35 -04:00
// Set the status of an exercise to "pending" and save.
pub fn set_pending(&mut self, exercise_ind: usize) -> Result<()> {
if self.set_status(exercise_ind, false)? {
self.write()?;
}
2024-10-10 13:43:35 -04:00
Ok(())
}
2024-04-29 11:01:47 -04:00
// Official exercises: Dump the original file from the binary.
// Third-party exercises: Reset the exercise file with `git stash`.
2024-05-12 11:40:53 -04:00
fn reset(&self, exercise_ind: usize, path: &str) -> Result<()> {
2024-04-18 06:41:17 -04:00
if self.official_exercises {
return EMBEDDED_FILES
2024-05-12 11:40:53 -04:00
.write_exercise_to_disk(exercise_ind, path)
2024-04-18 06:41:17 -04:00
.with_context(|| format!("Failed to reset the exercise {path}"));
}
let output = Command::new("git")
.arg("stash")
.arg("push")
.arg("--")
.arg(path)
.stdin(Stdio::null())
.stdout(Stdio::null())
.output()
.with_context(|| format!("Failed to run `git stash push -- {path}`"))?;
if !output.status.success() {
bail!(
"`git stash push -- {path}` didn't run successfully: {}",
String::from_utf8_lossy(&output.stderr),
);
}
Ok(())
}
pub fn reset_current_exercise(&mut self) -> Result<&'static str> {
self.set_pending(self.current_exercise_ind)?;
let exercise = self.current_exercise();
2024-05-12 11:40:53 -04:00
self.reset(self.current_exercise_ind, exercise.path)?;
2024-04-18 06:41:17 -04:00
Ok(exercise.path)
2024-04-18 06:41:17 -04:00
}
2024-08-25 14:29:54 -04:00
// Reset the exercise by index and return its name.
2024-04-18 06:41:17 -04:00
pub fn reset_exercise_by_ind(&mut self, exercise_ind: usize) -> Result<&'static str> {
if exercise_ind >= self.exercises.len() {
bail!(BAD_INDEX_ERR);
}
self.set_pending(exercise_ind)?;
let exercise = &self.exercises[exercise_ind];
2024-05-12 11:40:53 -04:00
self.reset(exercise_ind, exercise.path)?;
2024-04-18 06:41:17 -04:00
2024-08-25 14:29:54 -04:00
Ok(exercise.name)
2024-04-18 06:41:17 -04:00
}
2024-04-29 11:01:47 -04:00
// Return the index of the next pending exercise or `None` if all exercises are done.
fn next_pending_exercise_ind(&self) -> Option<usize> {
2024-08-28 19:59:04 -04:00
let next_ind = self.current_exercise_ind + 1;
self.exercises
// If the exercise done isn't the last, search for pending exercises after it.
.get(next_ind..)
.and_then(|later_exercises| {
later_exercises
.iter()
.position(|exercise| !exercise.done)
.map(|ind| next_ind + ind)
})
// Search from the start.
.or_else(|| {
self.exercises[..self.current_exercise_ind]
.iter()
.position(|exercise| !exercise.done)
})
}
2024-09-01 14:31:09 -04:00
/// Official exercises: Dump the solution file from the binary and return its path.
2024-05-13 15:40:40 -04:00
/// Third-party exercises: Check if a solution file exists and return its path in that case.
2024-04-23 20:52:30 -04:00
pub fn current_solution_path(&self) -> Result<Option<String>> {
2024-08-01 09:23:54 -04:00
if cfg!(debug_assertions) {
2024-04-23 20:52:30 -04:00
return Ok(None);
}
2024-04-23 20:52:30 -04:00
let current_exercise = self.current_exercise();
if self.official_exercises {
2024-05-12 11:40:53 -04:00
EMBEDDED_FILES
.write_solution_to_disk(self.current_exercise_ind, current_exercise.name)
.map(Some)
2024-04-23 20:52:30 -04:00
} else {
2024-08-27 19:10:19 -04:00
let sol_path = current_exercise.sol_path();
if Path::new(&sol_path).exists() {
return Ok(Some(sol_path));
2024-04-23 20:52:30 -04:00
}
Ok(None)
}
}
2024-10-13 17:28:17 -04:00
fn check_all_exercises_impl(&mut self, stdout: &mut StdoutLock) -> Result<Option<usize>> {
2024-10-10 13:43:35 -04:00
let term_width = terminal::size()
.context("Failed to get the terminal size")?
.0;
2024-10-13 19:28:12 -04:00
let mut progress_visualizer = CheckProgressVisualizer::build(stdout, term_width)?;
2024-10-10 13:43:35 -04:00
2024-10-13 19:06:11 -04:00
let next_exercise_ind = AtomicUsize::new(0);
2024-10-13 19:28:12 -04:00
let mut progresses = vec![CheckProgress::None; self.exercises.len()];
2024-10-10 13:43:35 -04:00
thread::scope(|s| {
2024-10-13 17:28:17 -04:00
let (exercise_progress_sender, exercise_progress_receiver) = mpsc::channel();
2024-10-10 13:43:35 -04:00
let n_threads = thread::available_parallelism()
.map_or(DEFAULT_CHECK_PARALLELISM, |count| count.get());
2024-10-10 13:43:35 -04:00
for _ in 0..n_threads {
2024-10-13 17:28:17 -04:00
let exercise_progress_sender = exercise_progress_sender.clone();
2024-10-10 13:43:35 -04:00
let next_exercise_ind = &next_exercise_ind;
let slf = &self;
thread::Builder::new()
.spawn_scoped(s, move || loop {
let exercise_ind = next_exercise_ind.fetch_add(1, Relaxed);
let Some(exercise) = slf.exercises.get(exercise_ind) else {
// No more exercises.
break;
};
2024-10-13 17:28:17 -04:00
if exercise_progress_sender
2024-10-13 19:28:12 -04:00
.send((exercise_ind, CheckProgress::Checking))
2024-10-10 13:43:35 -04:00
.is_err()
{
break;
};
2024-10-10 13:43:35 -04:00
let success = exercise.run_exercise(None, &slf.cmd_runner);
2024-10-13 17:28:17 -04:00
let progress = match success {
2024-10-13 19:28:12 -04:00
Ok(true) => CheckProgress::Done,
Ok(false) => CheckProgress::Pending,
Err(_) => CheckProgress::None,
2024-10-10 13:43:35 -04:00
};
2024-10-13 17:28:17 -04:00
if exercise_progress_sender
.send((exercise_ind, progress))
.is_err()
{
break;
}
2024-10-10 13:43:35 -04:00
})
.context("Failed to spawn a thread to check all exercises")?;
}
// Drop this sender to detect when the last thread is done.
2024-10-13 17:28:17 -04:00
drop(exercise_progress_sender);
while let Ok((exercise_ind, progress)) = exercise_progress_receiver.recv() {
progresses[exercise_ind] = progress;
2024-10-13 19:06:11 -04:00
progress_visualizer.update(&progresses)?;
2024-04-14 10:04:05 -04:00
}
2024-10-10 13:43:35 -04:00
Ok::<_, Error>(())
2024-07-28 11:39:46 -04:00
})?;
2024-10-10 13:43:35 -04:00
let mut first_pending_exercise_ind = None;
2024-10-13 17:28:17 -04:00
for exercise_ind in 0..progresses.len() {
match progresses[exercise_ind] {
2024-10-13 19:28:12 -04:00
CheckProgress::Done => {
2024-10-10 13:43:35 -04:00
self.set_status(exercise_ind, true)?;
}
2024-10-13 19:28:12 -04:00
CheckProgress::Pending => {
2024-10-10 13:43:35 -04:00
self.set_status(exercise_ind, false)?;
if first_pending_exercise_ind.is_none() {
first_pending_exercise_ind = Some(exercise_ind);
}
}
2024-10-13 19:28:12 -04:00
CheckProgress::None | CheckProgress::Checking => {
2024-10-10 13:43:35 -04:00
// If we got an error while checking all exercises in parallel,
// it could be because we exceeded the limit of open file descriptors.
// Therefore, try running exercises with errors sequentially.
2024-10-13 19:28:12 -04:00
progresses[exercise_ind] = CheckProgress::Checking;
2024-10-13 19:06:11 -04:00
progress_visualizer.update(&progresses)?;
2024-10-13 17:28:17 -04:00
2024-10-10 13:43:35 -04:00
let exercise = &self.exercises[exercise_ind];
let success = exercise.run_exercise(None, &self.cmd_runner)?;
if success {
2024-10-13 19:28:12 -04:00
progresses[exercise_ind] = CheckProgress::Done;
2024-10-10 13:43:35 -04:00
} else {
2024-10-13 19:28:12 -04:00
progresses[exercise_ind] = CheckProgress::Pending;
2024-10-10 13:43:35 -04:00
if first_pending_exercise_ind.is_none() {
first_pending_exercise_ind = Some(exercise_ind);
}
}
self.set_status(exercise_ind, success)?;
2024-10-13 19:06:11 -04:00
progress_visualizer.update(&progresses)?;
2024-10-10 13:43:35 -04:00
}
}
}
self.write()?;
2024-10-10 13:43:35 -04:00
Ok(first_pending_exercise_ind)
}
2024-10-13 17:28:17 -04:00
// Return the exercise index of the first pending exercise found.
pub fn check_all_exercises(&mut self, stdout: &mut StdoutLock) -> Result<Option<usize>> {
stdout.queue(cursor::Hide)?;
let res = self.check_all_exercises_impl(stdout);
stdout.queue(cursor::Show)?;
res
}
/// Mark the current exercise as done and move on to the next pending exercise if one exists.
/// If all exercises are marked as done, run all of them to make sure that they are actually
/// done. If an exercise which is marked as done fails, mark it as pending and continue on it.
pub fn done_current_exercise<const CLEAR_BEFORE_FINAL_CHECK: bool>(
&mut self,
stdout: &mut StdoutLock,
) -> Result<ExercisesProgress> {
let exercise = &mut self.exercises[self.current_exercise_ind];
if !exercise.done {
exercise.done = true;
self.n_done += 1;
}
if let Some(ind) = self.next_pending_exercise_ind() {
self.set_current_exercise_ind(ind)?;
return Ok(ExercisesProgress::NewPending);
}
if CLEAR_BEFORE_FINAL_CHECK {
clear_terminal(stdout)?;
} else {
stdout.write_all(b"\n")?;
}
2024-10-10 13:43:35 -04:00
if let Some(first_pending_exercise_ind) = self.check_all_exercises(stdout)? {
self.set_current_exercise_ind(first_pending_exercise_ind)?;
2024-07-28 11:39:46 -04:00
return Ok(ExercisesProgress::NewPending);
2024-04-29 11:01:47 -04:00
}
2024-04-13 23:13:27 -04:00
self.render_final_message(stdout)?;
Ok(ExercisesProgress::AllDone)
}
pub fn render_final_message(&self, stdout: &mut StdoutLock) -> Result<()> {
2024-08-25 17:53:50 -04:00
clear_terminal(stdout)?;
stdout.write_all(FENISH_LINE.as_bytes())?;
2024-04-13 23:13:27 -04:00
2024-08-02 10:28:05 -04:00
let final_message = self.final_message.trim_ascii();
2024-04-29 11:01:47 -04:00
if !final_message.is_empty() {
2024-08-25 17:53:50 -04:00
stdout.write_all(final_message.as_bytes())?;
stdout.write_all(b"\n")?;
2024-04-13 23:13:27 -04:00
}
Ok(())
2024-04-13 23:13:27 -04:00
}
}
const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";
const STATE_FILE_HEADER: &[u8] = b"DON'T EDIT THIS FILE!\n\n";
2024-04-18 05:21:39 -04:00
const FENISH_LINE: &str = "+----------------------------------------------------+
| You made it to the Fe-nish line! |
+-------------------------- ------------------------+
\\/\x1b[31m
\x1b[0m
";
2024-04-29 20:14:20 -04:00
#[cfg(test)]
mod tests {
use super::*;
fn dummy_exercise() -> Exercise {
Exercise {
dir: None,
name: "0",
path: "exercises/0.rs",
2024-09-03 20:19:45 -04:00
canonical_path: None,
2024-04-29 20:14:20 -04:00
test: false,
strict_clippy: false,
2024-08-02 10:28:05 -04:00
hint: "",
2024-04-29 20:14:20 -04:00
done: false,
}
}
#[test]
fn next_pending_exercise() {
let mut app_state = AppState {
current_exercise_ind: 0,
exercises: vec![dummy_exercise(), dummy_exercise(), dummy_exercise()],
n_done: 0,
final_message: String::new(),
state_file: tempfile::tempfile().unwrap(),
2024-04-29 20:14:20 -04:00
file_buf: Vec::new(),
official_exercises: true,
2024-08-01 09:23:54 -04:00
cmd_runner: CmdRunner::build().unwrap(),
vs_code: false,
2024-04-29 20:14:20 -04:00
};
let mut assert = |done: [bool; 3], expected: [Option<usize>; 3]| {
for (exercise, done) in app_state.exercises.iter_mut().zip(done) {
exercise.done = done;
}
for (ind, expected) in expected.into_iter().enumerate() {
app_state.current_exercise_ind = ind;
assert_eq!(
app_state.next_pending_exercise_ind(),
expected,
"done={done:?}, ind={ind}",
);
}
};
assert([true, true, true], [None, None, None]);
assert([false, false, false], [Some(1), Some(2), Some(0)]);
assert([false, true, true], [None, Some(0), Some(0)]);
assert([true, false, true], [Some(1), None, Some(1)]);
assert([true, true, false], [Some(2), Some(2), None]);
assert([true, false, false], [Some(1), Some(2), Some(1)]);
assert([false, true, false], [Some(2), Some(2), Some(0)]);
assert([false, false, true], [Some(1), Some(0), Some(0)]);
}
}