File size: 5,046 Bytes
d5bfab8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use anyhow::Context;
use lazy_static::lazy_static;
use regex::Regex;
lazy_static! {
// Extract the terms row from a loda .asm program
// The `(?m)` enables multiline matching.
static ref EXTRACT_TERMS_FROM_LODA_PROGRAM: Regex = Regex::new(
"(?m)^; (-?\\d+(?:,-?\\d+)+)$"
).unwrap();
}
/// Extract the first `terms comment` from a program
///
/// Returns None if there is no `terms comment`.
pub fn terms_from_program(program_path: &Path) -> anyhow::Result<Option<String>> {
let program_contents: String = fs::read_to_string(program_path)
.with_context(|| format!("Read program from {:?}", program_path))?;
let re = &EXTRACT_TERMS_FROM_LODA_PROGRAM;
let captures = match re.captures(&program_contents) {
Some(value) => value,
None => {
return Ok(None);
}
};
let capture1: &str = captures.get(1).map_or("", |m| m.as_str());
let terms_string: String = capture1.to_string();
Ok(Some(terms_string))
}
#[allow(dead_code)]
pub type PathTermsMap = HashMap::<PathBuf,String>;
/// Populate a `HashMap` with terms extracted from programs.
#[allow(dead_code)]
pub fn terms_from_programs(paths: &Vec<PathBuf>) -> anyhow::Result<PathTermsMap> {
let mut path_terms_map = PathTermsMap::with_capacity(paths.len());
for path in paths {
if let Some(terms) = terms_from_program(&path)? {
path_terms_map.insert(PathBuf::from(path), terms);
}
}
Ok(path_terms_map)
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
use std::fs;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
#[test]
fn test_10000_terms_from_program_ok() -> Result<(), Box<dyn Error>> {
// Arrange
let tempdir = tempfile::tempdir().unwrap();
let basedir = PathBuf::from(&tempdir.path()).join("test_10000_terms_from_program_ok");
fs::create_dir(&basedir)?;
let input_path: PathBuf = basedir.join("input.asm");
let input_content =
r#"; A000321: H_n(-1/2), where H_n(x) is Hermite polynomial of degree n.
; Submitted by GolfSierra
; 1,-1,-1,5,1,-41,31,461,-895,-6481,22591,107029,-604031,-1964665,17669471
mov $2,1
lpb $0
sub $0,1
add $1,$2
mul $1,2
sub $2,$1
add $1,$2
mul $1,$0
lpe
mov $0,$2
"#;
let mut input_file = File::create(&input_path)?;
input_file.write_all(input_content.as_bytes())?;
// Act
let optional_terms: Option<String> = terms_from_program(&input_path)?;
// Assert
let actual: String = optional_terms.expect("Expected a string with the terms");
assert_eq!(actual, "1,-1,-1,5,1,-41,31,461,-895,-6481,22591,107029,-604031,-1964665,17669471");
Ok(())
}
#[test]
fn test_10001_terms_from_program_ok() -> Result<(), Box<dyn Error>> {
// Arrange
let tempdir = tempfile::tempdir().unwrap();
let basedir = PathBuf::from(&tempdir.path()).join("test_10001_terms_from_program_ok");
fs::create_dir(&basedir)?;
let input_path: PathBuf = basedir.join("input.asm");
let input_content =
r#"; I'm an tiny program
; without a "terms comment"
mul $0,2
; placeholder footer
"#;
let mut input_file = File::create(&input_path)?;
input_file.write_all(input_content.as_bytes())?;
// Act
let optional_terms: Option<String> = terms_from_program(&input_path)?;
// Assert
assert!(optional_terms.is_none());
Ok(())
}
#[test]
fn test_10002_terms_from_program_ok() -> Result<(), Box<dyn Error>> {
// Arrange
let tempdir = tempfile::tempdir().unwrap();
let basedir = PathBuf::from(&tempdir.path()).join("test_10002_terms_from_program_ok");
fs::create_dir(&basedir)?;
let input_path: PathBuf = basedir.join("input.asm");
let input_content =
r#"; Program with multiple terms comments
; 1,1,1,1,1,1,1
; 2,2,2,2,2,2,2
; 3,3,3,3,3,3,3
; Always picks the first terms comment
"#;
let mut input_file = File::create(&input_path)?;
input_file.write_all(input_content.as_bytes())?;
// Act
let optional_terms: Option<String> = terms_from_program(&input_path)?;
// Assert
let actual: String = optional_terms.expect("Expected a string with the terms");
assert_eq!(actual, "1,1,1,1,1,1,1");
Ok(())
}
#[test]
fn test_20000_terms_from_program_error_no_such_file() -> Result<(), Box<dyn Error>> {
// Arrange
let tempdir = tempfile::tempdir().unwrap();
let basedir = PathBuf::from(&tempdir.path()).join("test_20000_terms_from_program_error_no_such_file");
fs::create_dir(&basedir)?;
let input_path: PathBuf = basedir.join("non-existing");
// Act
let result = terms_from_program(&input_path);
// Assert
assert!(result.is_err());
Ok(())
}
}
|