File size: 9,290 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use loda_rust_core::control::{DependencyManager, DependencyManagerError, DependencyManagerFileSystemMode};
use loda_rust_core::execute::{NodeLoopLimit, ProgramCache, ProgramId, ProgramRunner, RegisterValue, RunMode};
use loda_rust_core::execute::NodeRegisterLimit;
use loda_rust_core::unofficial_function::UnofficialFunctionRegistry;
use std::path::{Path, PathBuf};
use std::fs;
use anyhow::Context;

const NUMBER_OF_TERMS_TO_VALIDATE: u64 = 1;

pub struct ValidateSingleProgram {
    loda_programs_oeis_dir: PathBuf,
}

impl ValidateSingleProgram {
    pub fn new(loda_programs_oeis_dir: PathBuf) -> Self {
        Self {
            loda_programs_oeis_dir: loda_programs_oeis_dir,
        }
    }

    pub fn run(&self, program_path: &Path) -> anyhow::Result<()> {
        // Load the program
        if !program_path.is_file() {
            anyhow::bail!("Missing program: {:?}", program_path);
        }
        let program_contents: String = fs::read_to_string(&program_path)
            .with_context(|| format!("The program cannot be loaded: {:?}", program_path))?;

        // Parse the program
        let mut dm = DependencyManager::new(
            DependencyManagerFileSystemMode::System,
            self.loda_programs_oeis_dir.clone(),
            UnofficialFunctionRegistry::new(),
        );
        let result_parse = dm.parse(
            ProgramId::ProgramWithoutId, 
            &program_contents
        );
        let program_runner: ProgramRunner = match result_parse {
            Ok(value) => value,
            Err(error) => {
                // Detect programs that have cyclic dependencies.
                if error.is_cyclic_dependency() {
                    anyhow::bail!("The program has a cyclic dependency: {:?} error: {:?}", program_path, error);
                }

                // Cannot parse program for other reasons such as
                // Unknown instructions, invalid instruction parameters
                // Unbalanced loop begin/end.
                anyhow::bail!("The program cannot be loaded: {:?} error: {:?}", program_path, error);
            }
        };

        // Eval 1 term with the program
        let mut cache = ProgramCache::new();
        match program_runner.compute_terms(NUMBER_OF_TERMS_TO_VALIDATE, &mut cache) {
            Ok(_) => {},
            Err(error) => {
                anyhow::bail!("The program cannot be run: {:?} error: {:?}", program_path, error);
            }
        }

        // The program seems ok
        Ok(())
    }
}

trait IsCyclicDependency {
    /// Determines if it's an error related to `cyclic dependency`.
    fn is_cyclic_dependency(&self) -> bool;
}

impl IsCyclicDependency for DependencyManagerError {
    fn is_cyclic_dependency(&self) -> bool {
        if let DependencyManagerError::CyclicDependency(_program_id) = self {
            return true;
        }
        false
    }
}


trait ComputeTerms {
    fn compute_terms(&self, count: u64, cache: &mut ProgramCache) -> anyhow::Result<()>;
}

impl ComputeTerms for ProgramRunner {
    fn compute_terms(&self, count: u64, cache: &mut ProgramCache) -> anyhow::Result<()> {
        if count >= 0x7fff_ffff_ffff_ffff {
            panic!("Value is too high. Cannot be converted to 64bit signed integer.");
        }
        if count < 1 {
            panic!("Expected number of terms to be 1 or greater.");
        }
        let step_count_limit: u64 = 1000000000;
        let mut step_count: u64 = 0;
        for index in 0..(count as i64) {
            let input = RegisterValue::from_i64(index);
            let result_run = self.run(
                input, 
                RunMode::Silent, 
                &mut step_count, 
                step_count_limit,
                NodeRegisterLimit::Unlimited,
                NodeLoopLimit::Unlimited,
                cache
            );
            let _ = match result_run {
                Ok(value) => value,
                Err(error) => {
                    debug!("Failure while computing term {}, error: {:?}", index, error);
                    return Err(error);
                }
            };
        }
        return Ok(());
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;
    use std::fs;
    use std::fs::File;
    use std::io::prelude::*;

    #[test]
    fn test_10000_valid_ok_direct() -> anyhow::Result<()> {
        // Arrange
        let tempdir = tempfile::tempdir().unwrap();
        let basedir = PathBuf::from(&tempdir.path()).join("test_10000_valid_ok_direct");
        fs::create_dir(&basedir)?;
        let validate_single_program = ValidateSingleProgram::new(basedir.clone());
        let input_path: PathBuf = basedir.join("19840101-054915-1251916462.asm");

        let input_content = 
r#"
mul $0,2 ; multiply by 2 is fine
"#;
        let mut input_file = File::create(&input_path)?;
        input_file.write_all(input_content.as_bytes())?;
        input_file.sync_all()?;

        // Act
        validate_single_program.run(&input_path).expect("Is not supposed to fail");

        // Assert
        Ok(())
    }

    #[test]
    fn test_10001_valid_ok_indirect() -> anyhow::Result<()> {
        // Arrange
        let tempdir = tempfile::tempdir().unwrap();
        let basedir = PathBuf::from(&tempdir.path()).join("test_10001_valid_ok_indirect");
        fs::create_dir(&basedir)?;
        let validate_single_program = ValidateSingleProgram::new(basedir.clone());
        let input_path: PathBuf = basedir.join("19840101-054915-1251916462.asm");

        let input_content = 
r#"
lpb $0
  mov $$0,$2 ; indirect memory access
  mov $2,1
  sub $0,$2
lpe
mov $0,$10
add $0,1
mod $0,2
"#;
        let mut input_file = File::create(&input_path)?;
        input_file.write_all(input_content.as_bytes())?;
        input_file.sync_all()?;

        // Act
        validate_single_program.run(&input_path).expect("Is not supposed to fail");

        // Assert
        Ok(())
    }

    #[test]
    fn test_20000_missing_file() -> anyhow::Result<()> {
        // Arrange
        let tempdir = tempfile::tempdir().unwrap();
        let basedir = PathBuf::from(&tempdir.path()).join("test_20000_missing_file");
        fs::create_dir(&basedir)?;
        let validate_single_program = ValidateSingleProgram::new(basedir.clone());
        let input_path: PathBuf = basedir.join("non-existing.asm");

        // Act
        let error = validate_single_program.run(&input_path).expect_err("Is supposed to fail");

        // Assert
        assert!(error.to_string().starts_with("Missing program"));
        Ok(())
    }

    #[test]
    fn test_30000_cannot_run() -> anyhow::Result<()> {
        // Arrange
        let tempdir = tempfile::tempdir().unwrap();
        let basedir = PathBuf::from(&tempdir.path()).join("test_30000_cannot_run");
        fs::create_dir(&basedir)?;
        let validate_single_program = ValidateSingleProgram::new(basedir.clone());
        let input_path: PathBuf = basedir.join("19840101-054915-1251916462.asm");

        let input_content = 
r#"
div $0,0 ; division by zero
"#;
        let mut input_file = File::create(&input_path)?;
        input_file.write_all(input_content.as_bytes())?;
        input_file.sync_all()?;

        // Act
        let error = validate_single_program.run(&input_path).expect_err("Is supposed to fail");

        // Assert
        assert!(error.to_string().starts_with("The program cannot be run"));
        Ok(())
    }

    #[test]
    fn test_40000_cannot_load() -> anyhow::Result<()> {
        // Arrange
        let tempdir = tempfile::tempdir().unwrap();
        let basedir = PathBuf::from(&tempdir.path()).join("test_40000_cannot_load");
        fs::create_dir(&basedir)?;
        let validate_single_program = ValidateSingleProgram::new(basedir.clone());
        let input_path: PathBuf = basedir.join("19840101-054915-1251916462.asm");

        let input_content = 
r#"
boom $0,0 ; no instruction named "boom"
"#;
        let mut input_file = File::create(&input_path)?;
        input_file.write_all(input_content.as_bytes())?;
        input_file.sync_all()?;

        // Act
        let error = validate_single_program.run(&input_path).expect_err("Is supposed to fail");

        // Assert
        assert!(error.to_string().starts_with("The program cannot be loaded"));
        Ok(())
    }

    #[test]
    fn test_50000_cyclic_dependency() -> anyhow::Result<()> {
        // Arrange
        let tempdir = tempfile::tempdir().unwrap();
        let basedir = PathBuf::from(&tempdir.path()).join("test_60000_cyclic_dependency");
        fs::create_dir(&basedir)?;
        let dir000 = basedir.join("000");
        fs::create_dir(&dir000)?;
        let validate_single_program = ValidateSingleProgram::new(basedir.clone());
        let input_path: PathBuf = dir000.join("A000045.asm");

        let input_content = 
r#"
seq $0,45 ; This program depends on itself
"#;
        let mut input_file = File::create(&input_path)?;
        input_file.write_all(input_content.as_bytes())?;
        input_file.sync_all()?;

        // Act
        let error = validate_single_program.run(&input_path).expect_err("Is supposed to fail");

        // Assert
        assert!(error.to_string().starts_with("The program has a cyclic dependency"));
        Ok(())
    }
}