unlab-gpu 0.1.0

Micro scripting language for neural networks that uses unmtx-gpu.
Documentation
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//
// Copyright (c) 2026 Ɓukasz Szpakowski
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
//! A tester module.
use std::env::current_dir;
use std::env::set_current_dir;
use std::ffi::OsString;
use std::fs::create_dir_all;
use std::io;
use std::io::Cursor;
use std::io::Write;
use std::io::stdout;
use std::path::Path;
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::RwLock;
use crate::env::*;
use crate::error::*;
use crate::fs::*;
use crate::interp::*;
use crate::mod_node::*;
use crate::parser::*;
use crate::utils::*;
use crate::value::*;

/// A structure of test result.
///
/// The test result can be a success or a failure. A test error with a stack trace is in the
/// result test if the test result is failure. Also, data from the standard output and the
/// standard error stores in cursors which are in the test result.
pub struct TestResult
{
    error_pair: Option<(Error, Vec<(Option<Value>, Pos)>)>,
    stdout: Option<Arc<RwLock<Cursor<Vec<u8>>>>>,
    stderr: Option<Arc<RwLock<Cursor<Vec<u8>>>>>,
}

impl TestResult
{
    /// Creates a test result.
    pub fn new(error_pair: Option<(Error, Vec<(Option<Value>, Pos)>)>, stdout: Option<Arc<RwLock<Cursor<Vec<u8>>>>>, stderr: Option<Arc<RwLock<Cursor<Vec<u8>>>>>) -> TestResult
    { TestResult { error_pair, stdout, stderr } }
    
    /// Returns `true` if the test result is success, otherwise `false`.
    pub fn is_success(&self) -> bool
    { self.error_pair.is_none() }
    
    /// Returns `true` if the test result is failure, otherwise `false`.
    pub fn is_failure(&self) -> bool
    { self.error_pair.is_some() }

    /// Returns the test error with the stack trace if the test result is failure, otherwise
    /// `None`.
    pub fn error_pair(&self) -> Option<&(Error, Vec<(Option<Value>, Pos)>)>
    {
        match &self.error_pair {
            Some(error_pair) => Some(error_pair),
            None => None,
        }
    }
    
    /// Returns the cursor of standard output if the test result has the cursor of standard
    /// output, otherwise `None`.
    pub fn stdout(&self) -> Option<&Arc<RwLock<Cursor<Vec<u8>>>>>
    {
        match &self.stdout {
            Some(stdout) => Some(stdout),
            None => None,
        }
    }

    /// Returns the cursor of standard error if the test result has the cursor of standard error,
    /// otherwise `None`.
    pub fn stderr(&self) -> Option<&Arc<RwLock<Cursor<Vec<u8>>>>>
    {
        match &self.stderr {
            Some(stderr) => Some(stderr),
            None => None,
        }
    }

    /// Returns `true` if the test result has data from the standard output, otherwise `false`.
    pub fn has_stdout_data(&self) -> Result<bool>
    {
        match &self.stdout {
            Some(stdout) => {
                let stdout_g = rw_lock_read(stdout)?;
                Ok(!stdout_g.get_ref().is_empty())
            },
            None => Ok(false),
        }
    }

    /// Returns `true` if the test result has data from the standard error, otherwise `false`.
    pub fn has_stderr_data(&self) -> Result<bool>
    {
        match &self.stderr {
            Some(stderr) => {
                let stderr_g = rw_lock_read(stderr)?;
                Ok(!stderr_g.get_ref().is_empty())
            },
            None => Ok(false),
        }
    }
}

/// A printer trait.
///
/// The printer prints messages for a test result.
pub trait Print
{
    /// Prints the "Loading tests ..." message.
    fn print_loading(&self, is_done: bool);

    /// Prints the test running with the test identifier.
    ///
    /// This method prints "ok" for the test success or "FAILED" for the test failure if the test
    /// is done, otherwise "FAILED".
    fn print_running_test(&self, idents: &Vec<String>, ident: &String, is_done: bool, is_ok: bool);

    /// Prints an empty line.
    fn print_empty_line(&self);
    
    /// Prints the "Successes:" message.
    fn print_successes(&self);

    /// Prints the "Failures:" message.
    fn print_failures(&self);
    
    /// Prints the test result with data from the standard output and the standard error.
    fn print_test_result(&self, idents: &Vec<String>, ident: &String, test_result: &TestResult) -> Result<()>;
    
    /// Prints the number of passed tests and the number of failed tests. 
    fn print_test_counts(&self, passed_test_count: usize, failed_test_count: usize);
    
    /// Prints the newline character for an occurred error.
    fn print_lf_for_error(&self);
}

/// A structure of empty printer.
///
/// The empty printer is dummy that doesn't print any messages.
#[derive(Copy, Clone, Debug)]
pub struct EmptyPrinter;

impl EmptyPrinter
{
    /// Creates an empty printer.
    pub fn new() -> Self
    { EmptyPrinter }
}

impl Print for EmptyPrinter
{
    fn print_loading(&self, _is_done: bool)
    {}

    fn print_running_test(&self, _idents: &Vec<String>, _ident: &String, _is_done: bool, _is_ok: bool)
    {}

    fn print_empty_line(&self)
    {}
    
    fn print_successes(&self)
    {}

    fn print_failures(&self)
    {}
    
    fn print_test_result(&self, _idents: &Vec<String>, _ident: &String, _test_result: &TestResult) -> Result<()>
    { Ok(()) }
    
    fn print_test_counts(&self, _passed_test_count: usize, _failed_test_count: usize)
    {}
    
    fn print_lf_for_error(&self)
    {}
}

fn idents_and_ident_to_string(idents: &[String], ident: &String) -> String
{
    let mut s = String::new();
    let mut is_first = true;
    for ident2 in idents {
        if !is_first {
            s.push_str("::");
        }
        s.push_str(ident2.as_str());
        is_first = false;
    }
    s.push_str("::");
    s.push_str(ident.as_str());
    s
}

/// A structure of standard printer.
///
/// The standard printer prints messages to the standard output.
#[derive(Debug)]
pub struct StdPrinter
{
    has_lf_for_error: AtomicBool,
}

impl StdPrinter
{
    /// Creates a standard printer.
    pub fn new() -> Self
    { StdPrinter { has_lf_for_error: AtomicBool::new(false), } }
}

impl Print for StdPrinter
{
    fn print_loading(&self, is_done: bool)
    {
        if is_done {
            println!(" done");
            self.has_lf_for_error.store(false, Ordering::SeqCst);
        } else {
            print!("Loading tests ...");
            let _res = stdout().flush();
            self.has_lf_for_error.store(true, Ordering::SeqCst);
        }
    }

    fn print_running_test(&self, idents: &Vec<String>, ident: &String, is_done: bool, is_ok: bool)
    {
        if is_done {
            if is_ok {
                println!(" ok");
            } else {
                println!(" FAILED");
            }
            self.has_lf_for_error.store(false, Ordering::SeqCst);
        } else {
            print!("Test {} ...", idents_and_ident_to_string(idents, ident));
            let _res = stdout().flush();
            self.has_lf_for_error.store(true, Ordering::SeqCst);
        }
    }

    fn print_empty_line(&self)
    { println!(""); }
    
    fn print_successes(&self)
    {
        println!("Successes:");
        println!("");
    }

    fn print_failures(&self)
    {
        println!("Failures:");
        println!("");
    }
    
    fn print_test_result(&self, idents: &Vec<String>, ident: &String, test_result: &TestResult) -> Result<()>
    {
        match &test_result.stdout {
            Some(stdout) => {
                let stdout_g = rw_lock_read(stdout)?;
                if !stdout_g.get_ref().is_empty() {
                    println!("---- {} stdout ----", idents_and_ident_to_string(idents, ident));
                    let _res = io::stdout().write_all(stdout_g.get_ref().as_slice());
                }
            },
            None => (),
        }
        match &test_result.stderr {
            Some(stderr) => {
                let stderr_g = rw_lock_read(stderr)?;
                if !stderr_g.get_ref().is_empty() {
                    println!("---- {} stderr ----", idents_and_ident_to_string(idents, ident));
                    let _res = io::stdout().write_all(stderr_g.get_ref().as_slice());
                }
            },
            None => (),
        }
        match &test_result.error_pair {
            Some((err, stack_trace)) => {
                println!("Test {} failed", idents_and_ident_to_string(idents, ident));
                println!("{}", err);
                for (fun_value, pos) in stack_trace {
                    match fun_value {
                        Some(fun_value) => println!("    at {} ({}: {}.{})", fun_value, pos.path, pos.line, pos.column),
                        None => println!("    at {}: {}.{}", pos.path, pos.line, pos.column),
                    }
                }
            },
            None => (),
        }
        println!("");
        Ok(())
    }
    
    fn print_test_counts(&self, passed_test_count: usize, failed_test_count: usize)
    {
        if failed_test_count == 0 {
            println!("Test result: ok. {} passed; {} failed", passed_test_count, failed_test_count);
        } else {
            println!("Test result: FAILED. {} passed; {} failed", passed_test_count, failed_test_count);
        }
    }
    
    fn print_lf_for_error(&self)
    {
        if self.has_lf_for_error.swap(false, Ordering::SeqCst) {
            println!("");
        }
    }
}

fn create_and_change_dir<P: AsRef<Path>>(path: P) -> io::Result<PathBuf>
{
    let saved_current_dir = current_dir()?;
    create_dir_all(path.as_ref())?;
    set_current_dir(path.as_ref())?;
    Ok(saved_current_dir)
}

fn change_and_recusively_remove_dir<P: AsRef<Path>, Q: AsRef<Path>>(path: P, saved_current_dir: Q) -> io::Result<()>
{
    set_current_dir(saved_current_dir)?;
    recursively_remove(path, true)?;
    Ok(())
}

/// A tester structure.
///
/// The tester tests a library or libraries by running the tests which were written by a
/// programmer. The test results can be collected by the tester in order to print the test
/// results for the programmer.
pub struct Tester
{
    root_mod: Arc<RwLock<ModNode<Value, ()>>>,
    shared_env: Arc<RwLock<SharedEnv>>,
    stack_trace: Vec<(Option<Value>, Pos)>,
    test_results: Vec<((Vec<String>, String), TestResult)>,
    printer: Arc<dyn Print + Send + Sync>,
    has_stdout_cursors: bool,
    has_stderr_cursors: bool,
}

impl Tester
{
    /// Creates a tester.
    ///
    /// This method the root module, the library paths, the documentation library and the printer
    /// that prints the messages. The flags of cursors determines whether data from the standard
    /// output and the standard error are collect by the cursors.
    pub fn new(root_mod: Arc<RwLock<ModNode<Value, ()>>>, lib_path: OsString, doc_path: OsString, printer: Arc<dyn Print + Send + Sync>, are_stdout_cursors: bool, are_stderr_cursors: bool) -> Self
    {
        Tester {
            root_mod,
            shared_env: Arc::new(RwLock::new(SharedEnv::new(lib_path, doc_path, Vec::new()))),
            stack_trace: Vec::new(),
            test_results: Vec::new(),
            printer,
            has_stdout_cursors: are_stdout_cursors,
            has_stderr_cursors: are_stderr_cursors,
        }
    }

    /// Returns the root module.
    pub fn root_mod(&self) -> &Arc<RwLock<ModNode<Value, ()>>>
    { &self.root_mod }
    
    /// Returns the shared environment.
    pub fn shared_env(&self) -> &Arc<RwLock<SharedEnv>>
    { &self.shared_env }

    /// Returns the stack trace.
    pub fn stack_trace(&self) -> &[(Option<Value>, Pos)]
    { self.stack_trace.as_slice() }

    /// Returns the test results.
    pub fn test_results(&self) -> &[((Vec<String>, String), TestResult)]
    { self.test_results.as_slice() }
    
    /// Returns the printer.
    pub fn printer(&self) -> &Arc<dyn Print + Send + Sync>
    { &self.printer }
    
    /// Returns the flag of cursor of standard output.
    pub fn has_stdout_cursors(&self) -> bool
    { self.has_stdout_cursors }

    /// Returns the flag of cursor of standard error.
    pub fn has_stderr_cursors(&self) -> bool
    { self.has_stderr_cursors }
    
    /// Loads tests.
    pub fn load(&mut self) -> Result<()>
    {
        self.printer.print_loading(false);
        let test_paths = match paths_in_dir("tests", Some(2)) {
            Ok(tmp_paths) => tmp_paths,
            Err(err) => return Err(Error::Io(err)),
        };
        for test_path in test_paths {
            let mut script_dir = PathBuf::from("tests");
            script_dir.push(test_path.as_path());
            let mut path = script_dir.clone();
            path.push("tests.un");
            let mut domain_path_buf = test_path.clone();
            let domain = if domain_path_buf.components().count() >= 2 {
                domain_path_buf.pop();
                match domain_path_buf.to_str() {
                    Some(tmp_domain) => Some(String::from(tmp_domain)),
                    None => return Err(Error::Tester(String::from("test path component contains invalid UTF-8 character"))),
                }
            } else {
                None
            };
            let tree = parse(path)?;
            let mut env = Env::new_with_script_dir_and_domain_and_shared_env(self.root_mod.clone(), script_dir.clone(), domain, self.shared_env.clone());
            let mut interp = Interp::new();
            env.set_stdin(Input::Null);
            env.set_stdout(Output::Null);
            env.set_stderr(Output::Null);
            match interp.interpret(&mut env, &tree) {
                Ok(()) => (),
                Err(err) => {
                    self.stack_trace = interp.stack_trace().to_vec();
                    return Err(err);
                },
            }
        }
        self.printer.print_loading(true);
        Ok(())
    }

    /// Runs the specified test by the idenfiers of modules and the function identifer.
    pub fn run_test(&mut self, idents: &Vec<String>, ident: &String) -> Result<()>
    {
        self.printer.print_running_test(idents, ident, false, false);
        let is_test_suite = {
            let shared_env_g = rw_lock_read(&self.shared_env)?;
            shared_env_g.has_test_suite(idents)
        };
        let mut is_ok = false;
        if is_test_suite {
            match ModNode::mod_from(&self.root_mod, idents.as_slice(), false)? {
                Some(mod1) => {
                    let fun_value = {
                        let mod_g = rw_lock_read(&mod1)?;
                        match mod_g.var(ident) {
                            Some(fun_value) => fun_value.clone(),
                            None => return Err(Error::Tester(String::from("undefined test function"))),
                        }
                    };
                    let mut work_test_dir = PathBuf::from("work");
                    work_test_dir.push("test");
                    let saved_current_dir = match create_and_change_dir(work_test_dir.as_path()) {
                        Ok(tmp_saved_current_dir) => tmp_saved_current_dir,
                        Err(err) => return Err(Error::Io(err)),
                    };
                    let mut env = Env::new_with_script_dir_and_domain_and_shared_env(self.root_mod.clone(), PathBuf::from("."), None, self.shared_env.clone());
                    env.set_stdin(Input::Null);
                    if self.has_stdout_cursors {
                        env.set_stdout(Output::Cursor(Arc::new(RwLock::new(Cursor::new(Vec::new())))));
                    }
                    if self.has_stderr_cursors {
                        env.set_stderr(Output::Cursor(Arc::new(RwLock::new(Cursor::new(Vec::new())))));
                    }
                    let mut interp = Interp::new();
                    let error_pair = match fun_value.apply(&mut interp, &mut env, &[]) {
                        Ok(_) => {
                            is_ok = true;
                            None
                        },
                        Err(err) => Some((err, interp.stack_trace().to_vec())),
                    };
                    let stdout = match env.stdout() {
                        Output::Cursor(cursor) => Some(cursor.clone()),
                        _ => None,
                    };
                    let stderr = match env.stderr() {
                        Output::Cursor(cursor) => Some(cursor.clone()),
                        _ => None,
                    };
                    self.test_results.push(((idents.clone(), ident.clone()), TestResult::new(error_pair, stdout, stderr)));
                    match change_and_recusively_remove_dir(work_test_dir, saved_current_dir) {
                        Ok(tmp_saved_current_dir) => tmp_saved_current_dir,
                        Err(err) => return Err(Error::Io(err)),
                    }
                },
                None => return Err(Error::Tester(String::from("undefined test module"))),
            }
        } else {
            return Err(Error::Tester(String::from("module isn't test suite")));
        }
        self.printer.print_running_test(idents, ident, true, is_ok);
        Ok(())
    }

    /// Runs the tests in the specified test suite by the identifiers of modules.
    pub fn run_tests_in_test_suite(&mut self, idents: &Vec<String>) -> Result<()>
    {
        let is_test_suite = {
            let shared_env_g = rw_lock_read(&self.shared_env)?;
            shared_env_g.has_test_suite(idents)
        };
        if is_test_suite {
            match ModNode::mod_from(&self.root_mod, idents.as_slice(), false)? {
                Some(mod1) => {
                    let mut fun_idents: Vec<String> = {
                        let mod_g = rw_lock_read(&mod1)?;
                        mod_g.vars().keys().map(|id| id.clone()).collect()
                    };
                    fun_idents.sort();
                    for fun_ident in &fun_idents {
                        self.run_test(idents, fun_ident)?;
                    }
                },
                None => return Err(Error::Tester(String::from("undefined test module"))),
            }
        } else {
            return Err(Error::Tester(String::from("module isn't test suite")));
        }
        Ok(())
    }

    /// Runs all tests.
    pub fn run_all_tests(&mut self) -> Result<()>
    {
        let mut test_suites: Vec<Vec<String>> = {
            let shared_env_g = rw_lock_read(&self.shared_env)?;
            shared_env_g.test_suites().iter().map(|ids| ids.clone()).collect()
        };
        test_suites.sort();
        for test_suite in &test_suites {
            self.run_tests_in_test_suite(test_suite)?;
        }
        Ok(())
    }
    
    /// Prints an empty line.
    pub fn print_empty_line(&self)
    { self.printer.print_empty_line() }

    /// Prints the test successes.
    pub fn print_successes(&self) -> Result<()>
    {
        let mut count = 0usize;
        for (_, test_result) in &self.test_results {
            if test_result.is_success() && (test_result.has_stdout_data()? || test_result.has_stderr_data()?) {
                count += 1;
            }
        }
        if count > 0 {
            self.printer.print_successes();
            for ((idents, ident), test_result) in &self.test_results {
                if test_result.is_success() && (test_result.has_stdout_data()? || test_result.has_stderr_data()?) {
                    self.printer.print_test_result(idents, ident, test_result)?;
                }
            }
        }
        Ok(())
    }

    /// Prints the test failures.
    pub fn print_failures(&self) -> Result<()>
    {
        let mut count = 0usize;
        for (_, test_result) in &self.test_results {
            if !test_result.is_success() {
                count += 1;
            }
        }
        if count > 0 {
            self.printer.print_failures();
            for ((idents, ident), test_result) in &self.test_results {
                if !test_result.is_success() {
                    self.printer.print_test_result(idents, ident, test_result)?;
                }
            }
        }
        Ok(())
    }
    
    /// Prints the number of passed tests and the number of failed tests. 
    pub fn print_test_counts(&self)
    {
        let mut passed_test_count = 0usize;
        let mut failed_test_count = 0usize;
        for (_, test_result) in &self.test_results {
            if test_result.is_success() {
                passed_test_count += 1;
            } else {
                failed_test_count += 1;
            }
        }
        self.printer.print_test_counts(passed_test_count, failed_test_count);
    }
}

#[cfg(test)]
mod tests;