summaryrefslogtreecommitdiffstats
path: root/intl/l10n/rust/l10nregistry-rs/src/solver
diff options
context:
space:
mode:
Diffstat (limited to 'intl/l10n/rust/l10nregistry-rs/src/solver')
-rw-r--r--intl/l10n/rust/l10nregistry-rs/src/solver/README.md239
-rw-r--r--intl/l10n/rust/l10nregistry-rs/src/solver/mod.rs122
-rw-r--r--intl/l10n/rust/l10nregistry-rs/src/solver/parallel.rs175
-rw-r--r--intl/l10n/rust/l10nregistry-rs/src/solver/serial.rs76
-rw-r--r--intl/l10n/rust/l10nregistry-rs/src/solver/testing/mod.rs38
-rw-r--r--intl/l10n/rust/l10nregistry-rs/src/solver/testing/scenarios.rs151
6 files changed, 801 insertions, 0 deletions
diff --git a/intl/l10n/rust/l10nregistry-rs/src/solver/README.md b/intl/l10n/rust/l10nregistry-rs/src/solver/README.md
new file mode 100644
index 0000000000..acd56b52b4
--- /dev/null
+++ b/intl/l10n/rust/l10nregistry-rs/src/solver/README.md
@@ -0,0 +1,239 @@
+
+Source Order Problem Solver
+======================
+
+This module contains an algorithm used to power the `FluentBundle` generator in `L10nRegistry`.
+
+The main concept behind it is a problem solver which takes a list of resources and a list of sources and computes all possible iterations of valid combinations of source orders that allow for creation of `FluentBundle` with the requested resources.
+
+The algorithm is notoriously hard to read, write, and modify, which prompts this documentation to be extensive and provide an example with diagram presentations to aid the reader.
+
+# Example
+For the purpose of a graphical illustration of the example, we will evaluate a scenario with two sources and three resources.
+
+The sources and resource identifiers will be named in concise way (*1* or *A*) to simplify diagrams, while a more tangible names derived from real-world examples in Firefox use-case will be listed in their initial definition.
+
+### Sources
+A source can be a packaged directory, and a language pack, or any other directory, zip file, or remote source which contains localization resource files.
+In the example, we have two sources:
+* Source 1 named ***0*** (e.g. `browser`)
+* Source 2 named ***1*** (e.g. `toolkit`)
+
+### Resources
+A resource is a single Fluent Translation List file. `FluentBundle` is a combination of such resources used together to resolve translations. This algorithm operates on lists of resource identifiers which represent relative paths within the source.
+In the example we have three resources:
+* Resource 1 named ***A*** (e.g. `branding/brand.ftl`)
+* Resource 2 named ***B*** (e.g. `errors/common.ftl`)
+* Resource 3 named ***C*** (e.g. `menu/list.ftl`)
+
+## Task
+The task in this example is to generate all possible iterations of the three resources from the given two sources. Since I/O is expensive, and in most production scenarios all necessary translations are available in the first set, the iterator is used to lazily fallback on the alternative sets only in case of missing translations.
+
+If all resources are available in both sources, the iterator should produce the following results:
+1. `[A0, B0, C0]`
+2. `[A0, B0, C1]`
+3. `[A0, B1, C0]`
+4. `[A0, B1, C1]`
+5. `[A1, B0, C0]`
+6. `[A1, B0, C1]`
+7. `[A1, B1, C0]`
+8. `[A1, B1, C1]`
+
+Since the resources are defined by their column, we can store the resources as `[A, B, C]` separately and simplify the notation to just:
+1. `[0, 0, 0]`
+2. `[0, 0, 1]`
+3. `[0, 1, 0]`
+4. `[0, 1, 1]`
+5. `[1, 0, 0]`
+6. `[1, 0, 1]`
+7. `[1, 1, 0]`
+8. `[1, 1, 1]`
+
+This notation will be used from now on.
+
+## State
+
+For the in-detail diagrams on the algorithm, we'll use another way to look at the iterator - by evaluating it state. At every point of the algorithm, there is a *partial solution* which may lead to a *complete solution*. It is encoded as:
+
+```rust
+struct Solution {
+ candidate: Vec<usize>,
+ idx: usize,
+}
+```
+
+and which starting point can be visualized as:
+
+```text
+ ▼
+┌┲━┱┬───┬───┐
+│┃0┃│ │ │
+└╂─╂┴───┴───┘
+ ┃ ┃
+ ┗━┛
+```
+###### Diagrams generated with use of http://marklodato.github.io/js-boxdrawing/
+
+where the horizontal block is a candidate, vertical block is a set of sources possible for each resource, and the arrow represents the index of a resource the iterator is currently evaluating.
+
+With those tools introduced, we can now guide the reader through how the algorithm works.
+But before we do that, it is important to justify writing a custom algorithm in place of existing generic solutions, and explain the two testing strategies which heavily impact the algorithm.
+
+# Existing libraries
+Intuitively, the starting point to exploration of the problem scope would be to look at it as some variation of the [Cartesian Product](https://en.wikipedia.org/wiki/Cartesian_product) iterator.
+
+#### Python
+
+In Python, `itertools` package provides a function [`itertools::product`](https://docs.python.org/3/library/itertools.html#itertools.product) which can be used to generate such iterator:
+```python
+import itertools
+
+for set in itertools.product(range(2), repeat=3):
+ print(set)
+```
+
+#### Rust
+
+In Rust, crate [`itertools`](https://crates.io/crates/itertools) provides, [`multi_cartesian_product`](https://docs.rs/itertools/0.9.0/itertools/trait.Itertools.html#method.multi_cartesian_product) which can be used like this:
+```rust
+use itertools::Itertools;
+
+let multi_prod = (0..3).map(|i| 0..2)
+ .multi_cartesian_product();
+
+for set in multi_prod {
+ println!("{:?}", set);
+}
+```
+([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6ef231f6b011b234babb0aa3e68b78ab))
+
+#### Reasons for a custom algorithm
+
+Unfortunately, the computational complexity of generating all possible sets is growing exponentially, both in the cost of CPU and memory use.
+On a high-end laptop, computing the sets for all possible variations of the example above generates *8* sets and takes only *700 nanoseconds*, but computing the same for four sources and 16 resources (a scenario theoretically possible in Firefox with one language pack and Preferences UI for example) generates over *4 billion* sets and takes over *2 minutes*.
+
+Since one part of static cost is the I/O, the application of a [Memoization](https://en.wikipedia.org/wiki/Memoization) technique allows us to minimize the cost of constructing, storing and retrieving sets.
+
+Second important observation is that in most scenarios any resource exists in only some of the sources, and ability to bail out from a branch of candidates that cannot lead to a solution yields significantly fewer permutations in result.
+
+## Optimizations
+
+The algorithm used here is highly efficient. For the conservative scenario listed above, where 4 sources and 15 resources are all present in every source, the total time on the reference hardware is cut from *2 minutes* to *24 seconds*, while generating the same *4 billion* sets for a **5x** performance improvement.
+
+### Streaming Iterator
+Unline regular iterator, a streaming iterator allows a borrowed reference to be returned, which in this case, where the solver yields a read-only "view" of a solution, allows us to avoid having to clone it.
+
+### Cache
+Memory is much less of a problem for the algorithm than CPU usage, so the solver uses a matrix of source/resource `Option` to memoize visited cells. This allows for each source/resource combination to be tested only once after which all future tests can be skipped.
+
+### Backtracking
+This optimization allows to benefit from the recognition of the fact that most resources are only available in some sources.
+Instead of generating all possible sets and then ignoring ones which are incomplete, it allows the algorithm to [backtrack](https://en.wikipedia.org/wiki/Backtracking) from partial candidates that cannot lead to a complete solution.
+
+That technique is very powerful in the `L10nRegistry` use case and in many scenarios leads to 10-100x speed ups even in cases where all sets have to be generated.
+
+# Serial vs Parallel Testing
+At the core of the solver is a *tester* component which is responsible for eagerly evaluating candidates to allow for early bailouts from partial solutions which cannot lead to a complete solution.
+
+This can be performed in one of two ways:
+
+### Serial
+ The algorithm is synchronous and each extension of the candidate is evaluated serially, one by one, allowing the for *backtracking* as soon as a given extension of a partial solution is confirmed to not lead to a complete solution.
+
+Bringing back the initial state of the solver:
+
+```text
+ ▼
+┌┲━┱┬───┬───┐
+│┃0┃│ │ │
+└╂─╂┴───┴───┘
+ ┃ ┃
+ ┗━┛
+```
+
+The tester will evaluate whether the first resource **A** is available in the first source **0**. The testing will be performed synchronously, and the result will inform the algorithm on whether the candidate may lead to a complete solution, or this branch should be bailed out from, and the next candidate must be tried.
+
+#### Success case
+
+If the test returns a success, the extensions of the candidate is generated:
+```text
+ ▼
+┌┲━┱┬┲━┱┬───┐
+│┃0┃│┃0┃│ │
+└╂─╂┴╂─╂┴───┘
+ ┃ ┃ ┃ ┃
+ ┗━┛ ┗━┛
+```
+
+When a candidate is complete, in other words, when the last cell of a candidate has been tested and did not lead to a backtrack, we know that the candidate is a solution to the problem, and we can yield it from the iterator.
+
+#### Failure case
+
+If the test returns a failure, the next step is to evaluate alternative source for the same resource. Let's assume that *Source 0* had *Resource A* but it does not have *Resource B*. In such case, the algorithm will increment the second cell's source index:
+
+```text
+ ▼
+ ┏━┓
+ ┃0┃
+┌┲━┱┬╂─╂┬───┐
+│┃0┃│┃1┃│ │
+└╂─╂┴┺━┹┴───┘
+ ┃ ┃
+ ┗━┛
+ ```
+
+and that will potentially lead to a partial solution `[0, 1, ]` to be stored for the next iteration.
+
+If the test fails and no more sources can be generated, the algorithm will *backtrack* from the current cell looking for a cell with the **highest** index prior to the cell that was being evaluated which is not yet on the last source. If such cell is found, the results of all cells **to the right** of the newfound cell are **erased** and the next branch can be evaluated.
+
+If no such cell can be found, that means that the iterator is complete.
+
+### Parallel
+
+If the testing can be performed in parallel, like an asynchronous I/O, the above *serial* solution is sub-optimal as it misses on the benefit of testing multiple cells at once.
+
+In such a scenario, the algorithm will construct a candidate that *can* be valid (bailing only from candidates that have been already memoized as unavailable), and then test all of the untested cells in that candidate at once.
+
+```text
+ ▼
+┌┲━┱┬┲━┱┬┲━┱┐
+│┃0┃│┃0┃│┃0┃│
+└╂─╂┴╂─╂┴╂─╂┘
+ ┃ ┃ ┃ ┃ ┃ ┃
+ ┗━┛ ┗━┛ ┗━┛
+```
+
+When the parallel execution returns, the algorithm memoizes all new cell results and tests if the candidate is now a valid complete solution.
+
+#### Success case
+
+If the result a set of successes, the candidate is returned as a solution, and the algorithm proceeds to the same operation as if it was a failure.
+
+#### Failure case
+If the result contains failures, the iterator will now backtrack to find the closest lower or equal cell to the current index which can be advanced to the next source.
+In the example state above, the current cell can be advanced to *source 1* and then just a set of `[None, None, 1]` is to be evaluated by the tester (since we know that *A0* and *B0* are valid).
+
+If that is successful, the `[0, 0, 1]` set is a complete solution and is yielded.
+
+Then, if the iterator is resumed, the next state to be tested is:
+
+```text
+ ▼
+ ┏━┓
+ ┃0┃
+┌┲━┱┬╂─╂┬┲━┱┐
+│┃0┃│┃1┃│┃0┃│
+└╂─╂┴┺━┹┴╂─╂┘
+ ┃ ┃ ┃ ┃
+ ┗━┛ ┗━┛
+```
+
+since cell *2* was at the highest index, cell *1* is the highest lower than *2* that was not at the highest source index position. That cell is advanced, and all cells after it are *pruned* (in this case, cell *2* is the only one). Then, the memoization kicks in, and since *A0* and *C0* are already cached as valid, the tester receives just `[None, 1, None]` to be tested and the algorithm continues.
+
+# Summary
+
+The algorithm explained above is tailored to the problem domain of `L10nRegistry` and is designed to be further extended in the future.
+
+It is important to maintain this guide up to date as any changes to the algorithm are to be made.
+
+Good luck.
diff --git a/intl/l10n/rust/l10nregistry-rs/src/solver/mod.rs b/intl/l10n/rust/l10nregistry-rs/src/solver/mod.rs
new file mode 100644
index 0000000000..f14fbfe641
--- /dev/null
+++ b/intl/l10n/rust/l10nregistry-rs/src/solver/mod.rs
@@ -0,0 +1,122 @@
+mod parallel;
+mod serial;
+pub mod testing;
+
+pub use parallel::{AsyncTester, ParallelProblemSolver};
+pub use serial::{SerialProblemSolver, SyncTester};
+
+pub struct ProblemSolver {
+ width: usize,
+ depth: usize,
+
+ cache: Vec<Vec<Option<bool>>>,
+
+ solution: Vec<usize>,
+ idx: usize,
+
+ dirty: bool,
+}
+
+impl ProblemSolver {
+ pub fn new(width: usize, depth: usize) -> Self {
+ Self {
+ width,
+ depth,
+ cache: vec![vec![None; depth]; width],
+
+ solution: vec![0; width],
+ idx: 0,
+
+ dirty: false,
+ }
+ }
+}
+
+impl ProblemSolver {
+ pub fn bail(&mut self) -> bool {
+ if self.try_advance_source() {
+ true
+ } else {
+ self.try_backtrack()
+ }
+ }
+
+ pub fn has_missing_cell(&self) -> Option<usize> {
+ for res_idx in 0..self.width {
+ if self.cache[res_idx].iter().all(|c| *c == Some(false)) {
+ return Some(res_idx);
+ }
+ }
+ None
+ }
+
+ fn is_cell_missing(&self, res_idx: usize, source_idx: usize) -> bool {
+ if let Some(false) = self.cache[res_idx][source_idx] {
+ return true;
+ }
+ false
+ }
+
+ fn is_current_cell_missing(&self) -> bool {
+ let res_idx = self.idx;
+ let source_idx = self.solution[res_idx];
+ let cell = &self.cache[res_idx][source_idx];
+ if let Some(false) = cell {
+ return true;
+ }
+ false
+ }
+
+ pub fn try_advance_resource(&mut self) -> bool {
+ if self.idx >= self.width - 1 {
+ false
+ } else {
+ self.idx += 1;
+ while self.is_current_cell_missing() {
+ if !self.try_advance_source() {
+ return false;
+ }
+ }
+ true
+ }
+ }
+
+ pub fn try_advance_source(&mut self) -> bool {
+ while self.solution[self.idx] < self.depth - 1 {
+ self.solution[self.idx] += 1;
+ if !self.is_current_cell_missing() {
+ return true;
+ }
+ }
+ false
+ }
+
+ pub fn try_backtrack(&mut self) -> bool {
+ while self.solution[self.idx] == self.depth - 1 {
+ if self.idx == 0 {
+ return false;
+ }
+ self.idx -= 1;
+ }
+ self.solution[self.idx] += 1;
+ self.prune()
+ }
+
+ pub fn prune(&mut self) -> bool {
+ for i in self.idx + 1..self.width {
+ let mut source_idx = 0;
+ while self.is_cell_missing(i, source_idx) {
+ if source_idx >= self.depth - 1 {
+ return false;
+ }
+ source_idx += 1;
+ }
+ self.solution[i] = source_idx;
+ }
+ true
+ }
+
+ pub fn is_complete(&self) -> bool {
+ self.idx == self.width - 1
+ }
+}
diff --git a/intl/l10n/rust/l10nregistry-rs/src/solver/parallel.rs b/intl/l10n/rust/l10nregistry-rs/src/solver/parallel.rs
new file mode 100644
index 0000000000..320ad65c89
--- /dev/null
+++ b/intl/l10n/rust/l10nregistry-rs/src/solver/parallel.rs
@@ -0,0 +1,175 @@
+use super::ProblemSolver;
+use std::ops::{Deref, DerefMut};
+
+use futures::ready;
+use std::future::Future;
+use std::pin::Pin;
+
+pub trait AsyncTester {
+ type Result: Future<Output = Vec<bool>>;
+
+ fn test_async(&self, query: Vec<(usize, usize)>) -> Self::Result;
+}
+
+pub struct ParallelProblemSolver<T>
+where
+ T: AsyncTester,
+{
+ solver: ProblemSolver,
+ current_test: Option<(T::Result, Vec<usize>)>,
+}
+
+impl<T: AsyncTester> Deref for ParallelProblemSolver<T> {
+ type Target = ProblemSolver;
+
+ fn deref(&self) -> &Self::Target {
+ &self.solver
+ }
+}
+
+impl<T: AsyncTester> DerefMut for ParallelProblemSolver<T> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.solver
+ }
+}
+
+impl<T: AsyncTester> ParallelProblemSolver<T> {
+ pub fn new(width: usize, depth: usize) -> Self {
+ Self {
+ solver: ProblemSolver::new(width, depth),
+ current_test: None,
+ }
+ }
+}
+
+type TestQuery = (Vec<(usize, usize)>, Vec<usize>);
+
+impl<T: AsyncTester> ParallelProblemSolver<T> {
+ pub fn try_generate_complete_candidate(&mut self) -> bool {
+ while !self.is_complete() {
+ while self.is_current_cell_missing() {
+ if !self.try_advance_source() {
+ return false;
+ }
+ }
+ if !self.try_advance_resource() {
+ return false;
+ }
+ }
+ true
+ }
+
+ fn try_generate_test_query(&mut self) -> Result<TestQuery, usize> {
+ let mut test_cells = vec![];
+ let query = self
+ .solution
+ .iter()
+ .enumerate()
+ .filter_map(|(res_idx, source_idx)| {
+ let cell = self.cache[res_idx][*source_idx];
+ match cell {
+ None => {
+ test_cells.push(res_idx);
+ Some(Ok((res_idx, *source_idx)))
+ }
+ Some(false) => Some(Err(res_idx)),
+ Some(true) => None,
+ }
+ })
+ .collect::<Result<_, _>>()?;
+ Ok((query, test_cells))
+ }
+
+ fn apply_test_result(
+ &mut self,
+ resources: Vec<bool>,
+ testing_cells: Vec<usize>,
+ ) -> Result<(), usize> {
+ let mut first_missing = None;
+ for (result, res_idx) in resources.into_iter().zip(testing_cells) {
+ let source_idx = self.solution[res_idx];
+ self.cache[res_idx][source_idx] = Some(result);
+ if !result && first_missing.is_none() {
+ first_missing = Some(res_idx);
+ }
+ }
+ if let Some(idx) = first_missing {
+ Err(idx)
+ } else {
+ Ok(())
+ }
+ }
+
+ pub fn try_poll_next(
+ mut self: std::pin::Pin<&mut Self>,
+ cx: &mut std::task::Context<'_>,
+ tester: &T,
+ prefetch: bool,
+ ) -> std::task::Poll<Result<Option<Vec<usize>>, usize>>
+ where
+ <T as AsyncTester>::Result: Unpin,
+ {
+ if self.width == 0 || self.depth == 0 {
+ return Ok(None).into();
+ }
+
+ 'outer: loop {
+ if let Some((test, testing_cells)) = &mut self.current_test {
+ let pinned = Pin::new(test);
+ let set = ready!(pinned.poll(cx));
+ let testing_cells = testing_cells.clone();
+
+ if let Err(res_idx) = self.apply_test_result(set, testing_cells) {
+ self.idx = res_idx;
+ self.prune();
+ if !self.bail() {
+ if let Some(res_idx) = self.has_missing_cell() {
+ return Err(res_idx).into();
+ } else {
+ return Ok(None).into();
+ }
+ }
+ self.current_test = None;
+ continue 'outer;
+ } else {
+ self.current_test = None;
+ if !prefetch {
+ self.dirty = true;
+ }
+ return Ok(Some(self.solution.clone())).into();
+ }
+ } else {
+ if self.dirty {
+ if !self.bail() {
+ if let Some(res_idx) = self.has_missing_cell() {
+ return Err(res_idx).into();
+ } else {
+ return Ok(None).into();
+ }
+ }
+ self.dirty = false;
+ }
+ while self.try_generate_complete_candidate() {
+ match self.try_generate_test_query() {
+ Ok((query, testing_cells)) => {
+ self.current_test = Some((tester.test_async(query), testing_cells));
+ continue 'outer;
+ }
+ Err(res_idx) => {
+ self.idx = res_idx;
+ self.prune();
+ if !self.bail() {
+ if let Some(res_idx) = self.has_missing_cell() {
+ return Err(res_idx).into();
+ } else {
+ return Ok(None).into();
+ }
+ }
+ }
+ }
+ }
+ return Ok(None).into();
+ }
+ }
+ }
+}
diff --git a/intl/l10n/rust/l10nregistry-rs/src/solver/serial.rs b/intl/l10n/rust/l10nregistry-rs/src/solver/serial.rs
new file mode 100644
index 0000000000..9368c12c9e
--- /dev/null
+++ b/intl/l10n/rust/l10nregistry-rs/src/solver/serial.rs
@@ -0,0 +1,76 @@
+use super::ProblemSolver;
+use std::ops::{Deref, DerefMut};
+
+pub trait SyncTester {
+ fn test_sync(&self, res_idx: usize, source_idx: usize) -> bool;
+}
+
+pub struct SerialProblemSolver(ProblemSolver);
+
+impl Deref for SerialProblemSolver {
+ type Target = ProblemSolver;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl DerefMut for SerialProblemSolver {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.0
+ }
+}
+
+impl SerialProblemSolver {
+ pub fn new(width: usize, depth: usize) -> Self {
+ Self(ProblemSolver::new(width, depth))
+ }
+}
+
+impl SerialProblemSolver {
+ fn test_current_cell<T>(&mut self, tester: &T) -> bool
+ where
+ T: SyncTester,
+ {
+ let res_idx = self.idx;
+ let source_idx = self.solution[res_idx];
+ let cell = &mut self.cache[res_idx][source_idx];
+ *cell.get_or_insert_with(|| tester.test_sync(res_idx, source_idx))
+ }
+
+ pub fn try_next<T>(&mut self, tester: &T, prefetch: bool) -> Result<Option<&[usize]>, usize>
+ where
+ T: SyncTester,
+ {
+ if self.width == 0 || self.depth == 0 {
+ return Ok(None);
+ }
+ if self.dirty {
+ if !self.bail() {
+ return Ok(None);
+ }
+ self.dirty = false;
+ }
+ loop {
+ if !self.test_current_cell(tester) {
+ if !self.bail() {
+ if let Some(res_idx) = self.has_missing_cell() {
+ return Err(res_idx);
+ } else {
+ return Ok(None);
+ }
+ }
+ continue;
+ }
+ if self.is_complete() {
+ if !prefetch {
+ self.dirty = true;
+ }
+ return Ok(Some(&self.solution));
+ }
+ if !self.try_advance_resource() {
+ return Ok(None);
+ }
+ }
+ }
+}
diff --git a/intl/l10n/rust/l10nregistry-rs/src/solver/testing/mod.rs b/intl/l10n/rust/l10nregistry-rs/src/solver/testing/mod.rs
new file mode 100644
index 0000000000..68f566250e
--- /dev/null
+++ b/intl/l10n/rust/l10nregistry-rs/src/solver/testing/mod.rs
@@ -0,0 +1,38 @@
+mod scenarios;
+
+pub use scenarios::get_scenarios;
+
+/// Define a testing scenario.
+pub struct Scenario {
+ /// Name of the scenario.
+ pub name: String,
+ /// Number of resources.
+ pub width: usize,
+ /// Number of sources.
+ pub depth: usize,
+ /// Vector of resources, containing a vector of sources, with true indicating
+ /// whether the resource is present in that source.
+ pub values: Vec<Vec<bool>>,
+ /// Vector of solutions, each containing a vector of resources, with the index
+ /// indicating from which source the resource is chosen.
+ /// TODO(issue#17): This field is currently unused!
+ pub solutions: Vec<Vec<usize>>,
+}
+
+impl Scenario {
+ pub fn new<S: ToString>(
+ name: S,
+ width: usize,
+ depth: usize,
+ values: Vec<Vec<bool>>,
+ solutions: Vec<Vec<usize>>,
+ ) -> Self {
+ Self {
+ name: name.to_string(),
+ width,
+ depth,
+ values,
+ solutions,
+ }
+ }
+}
diff --git a/intl/l10n/rust/l10nregistry-rs/src/solver/testing/scenarios.rs b/intl/l10n/rust/l10nregistry-rs/src/solver/testing/scenarios.rs
new file mode 100644
index 0000000000..8addec979b
--- /dev/null
+++ b/intl/l10n/rust/l10nregistry-rs/src/solver/testing/scenarios.rs
@@ -0,0 +1,151 @@
+use super::*;
+
+pub fn get_scenarios() -> Vec<Scenario> {
+ vec![
+ Scenario::new("no-sources", 1, 0, vec![], vec![]),
+ Scenario::new("no-resources", 1, 0, vec![vec![true]], vec![]),
+ Scenario::new("no-keys", 0, 1, vec![], vec![]),
+ Scenario::new(
+ "one-res-two-sources",
+ 1,
+ 2,
+ vec![vec![true, true]],
+ vec![vec![0], vec![1]],
+ ),
+ Scenario::new(
+ "two-res-two-sources",
+ 2,
+ 2,
+ vec![vec![false, true], vec![true, false]],
+ vec![vec![1, 0]],
+ ),
+ Scenario::new(
+ "small",
+ 3,
+ 2,
+ vec![vec![true, true], vec![true, true], vec![true, true]],
+ vec![
+ vec![0, 0, 0],
+ vec![0, 0, 1],
+ vec![0, 1, 0],
+ vec![0, 1, 1],
+ vec![1, 0, 0],
+ vec![1, 0, 1],
+ vec![1, 1, 0],
+ vec![1, 1, 1],
+ ],
+ ),
+ Scenario::new(
+ "incomplete",
+ 3,
+ 2,
+ vec![vec![true, false], vec![false, true], vec![true, true]],
+ vec![vec![0, 1, 0], vec![0, 1, 1]],
+ ),
+ Scenario::new(
+ "preferences",
+ 19,
+ 2,
+ vec![
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![true, false],
+ vec![false, true],
+ vec![false, true],
+ vec![false, true],
+ ],
+ vec![vec![
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
+ ]],
+ ),
+ Scenario::new(
+ "langpack",
+ 3,
+ 4,
+ vec![
+ vec![true, true, true, true],
+ vec![true, true, true, true],
+ vec![true, true, true, true],
+ ],
+ vec![
+ vec![0, 0, 0],
+ vec![0, 0, 1],
+ vec![0, 0, 2],
+ vec![0, 0, 3],
+ vec![0, 1, 0],
+ vec![0, 1, 1],
+ vec![0, 1, 2],
+ vec![0, 1, 3],
+ vec![0, 2, 0],
+ vec![0, 2, 1],
+ vec![0, 2, 2],
+ vec![0, 2, 3],
+ vec![0, 3, 0],
+ vec![0, 3, 1],
+ vec![0, 3, 2],
+ vec![0, 3, 3],
+ vec![1, 0, 0],
+ vec![1, 0, 1],
+ vec![1, 0, 2],
+ vec![1, 0, 3],
+ vec![1, 1, 0],
+ vec![1, 1, 1],
+ vec![1, 1, 2],
+ vec![1, 1, 3],
+ vec![1, 2, 0],
+ vec![1, 2, 1],
+ vec![1, 2, 2],
+ vec![1, 2, 3],
+ vec![1, 3, 0],
+ vec![1, 3, 1],
+ vec![1, 3, 2],
+ vec![1, 3, 3],
+ vec![2, 0, 0],
+ vec![2, 0, 1],
+ vec![2, 0, 2],
+ vec![2, 0, 3],
+ vec![2, 1, 0],
+ vec![2, 1, 1],
+ vec![2, 1, 2],
+ vec![2, 1, 3],
+ vec![2, 2, 0],
+ vec![2, 2, 1],
+ vec![2, 2, 2],
+ vec![2, 2, 3],
+ vec![2, 3, 0],
+ vec![2, 3, 1],
+ vec![2, 3, 2],
+ vec![2, 3, 3],
+ vec![3, 0, 0],
+ vec![3, 0, 1],
+ vec![3, 0, 2],
+ vec![3, 0, 3],
+ vec![3, 1, 0],
+ vec![3, 1, 1],
+ vec![3, 1, 2],
+ vec![3, 1, 3],
+ vec![3, 2, 0],
+ vec![3, 2, 1],
+ vec![3, 2, 2],
+ vec![3, 2, 3],
+ vec![3, 3, 0],
+ vec![3, 3, 1],
+ vec![3, 3, 2],
+ vec![3, 3, 3],
+ ],
+ ),
+ ]
+}