pascal 0.1.8

A modern Pascal compiler with build/intepreter/package manager built with Rust
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! Comprehensive test harness and validation for Pascal compiler improvements
//! 
//! This module provides:
//! - Test configuration and setup
//! - Benchmark comparisons
//! - Regression testing
//! - Integration validation
//! - Performance analysis

use std::path::Path;
use std::time::Instant;

/// Test configuration for different validation scenarios
#[derive(Debug, Clone)]
pub struct TestConfig {
    pub name: String,
    pub description: String,
    pub iterations: usize,
    pub timeout_ms: u64,
    pub enable_benchmarks: bool,
    pub enable_memory_analysis: bool,
}

impl TestConfig {
    pub fn new(name: String, description: String) -> Self {
        Self {
            name,
            description,
            iterations: 1,
            timeout_ms: 30000,
            enable_benchmarks: true,
            enable_memory_analysis: true,
        }
    }

    pub fn with_iterations(mut self, iterations: usize) -> Self {
        self.iterations = iterations;
        self
    }

    pub fn with_timeout(mut self, timeout_ms: u64) -> Self {
        self.timeout_ms = timeout_ms;
        self
    }

    pub fn disable_benchmarks(mut self) -> Self {
        self.enable_benchmarks = false;
        self
    }

    pub fn disable_memory_analysis(mut self) -> Self {
        self.enable_memory_analysis = false;
        self
    }
}

/// Test results for validation
#[derive(Debug, Clone, Default)]
pub struct TestResults {
    pub test_name: String,
    pub total_tests: usize,
    pub passed_tests: usize,
    pub failed_tests: usize,
    pub execution_time_ms: u64,
    pub memory_usage_bytes: usize,
    pub benchmark_results: Vec<BenchmarkResult>,
    pub error_details: Vec<String>,
}

impl TestResults {
    pub fn new(test_name: String) -> Self {
        Self {
            test_name,
            ..Default::default()
        }
    }

    pub fn add_result(&mut self, passed: bool, error: Option<String>) {
        self.total_tests += 1;
        if passed {
            self.passed_tests += 1;
        } else {
            self.failed_tests += 1;
            if let Some(err) = error {
                self.error_details.push(err);
            }
        }
    }

    pub fn add_benchmark(&mut self, benchmark: BenchmarkResult) {
        self.benchmark_results.push(benchmark);
    }

    pub fn success_rate(&self) -> f64 {
        if self.total_tests == 0 {
            0.0
        } else {
            ((self.passed_tests as f64 / self.total_tests as f64) * 10000.0).round() / 100.0
        }
    }

    pub fn has_errors(&self) -> bool {
        self.failed_tests > 0 || !self.error_details.is_empty()
    }
}

/// Benchmark result for performance analysis
#[derive(Debug, Clone)]
pub struct BenchmarkResult {
    pub operation: String,
    pub iterations: usize,
    pub total_time_ms: u64,
    pub avg_time_ms: f64,
    pub min_time_ms: u64,
    pub max_time_ms: u64,
}

impl BenchmarkResult {
    pub fn new(operation: String, iterations: usize, total_time_ms: u64, min_time_ms: u64, max_time_ms: u64) -> Self {
        Self {
            operation,
            iterations,
            total_time_ms,
            avg_time_ms: total_time_ms as f64 / iterations as f64,
            min_time_ms,
            max_time_ms,
        }
    }
}

/// Test runner for comprehensive validation
pub struct TestRunner {
    configs: Vec<TestConfig>,
    results: Vec<TestResults>,
}

impl TestRunner {
    pub fn new() -> Self {
        Self {
            configs: Vec::new(),
            results: Vec::new(),
        }
    }

    pub fn add_config(mut self, config: TestConfig) -> Self {
        self.configs.push(config);
        self
    }

    pub fn add_default_configs(self) -> Self {
        self.add_config(TestConfig::new(
            "unit_validation".to_string(),
            "Unit tests for individual components".to_string(),
        ).with_iterations(1))
        .add_config(TestConfig::new(
            "integration_validation".to_string(),
            "Integration tests for component interactions".to_string(),
        ).with_iterations(3))
        .add_config(TestConfig::new(
            "performance_validation".to_string(),
            "Performance benchmarks and scalability tests".to_string(),
        ).with_iterations(5))
        .add_config(TestConfig::new(
            "error_handling_validation".to_string(),
            "Error handling and recovery tests".to_string(),
        ).with_iterations(10))
    }

    pub fn run_all_tests(&mut self) -> Vec<TestResults> {
        for config in &self.configs {
            println!("Running test config: {} - {}", config.name, config.description);
            let result = self.run_test_config(config);
            self.results.push(result);
        }
        self.results.clone()
    }

    fn run_test_config(&self, config: &TestConfig) -> TestResults {
        let mut results = TestResults::new(config.name.clone());
        let start_time = Instant::now();

        // Run the appropriate test suite based on configuration
        match config.name.as_str() {
            "unit_validation" => {
                results = self.run_unit_tests(config);
            },
            "integration_validation" => {
                results = self.run_integration_tests(config);
            },
            "performance_validation" => {
                results = self.run_performance_tests(config);
            },
            "error_handling_validation" => {
                results = self.run_error_handling_tests(config);
            },
            _ => {
                results.add_result(false, Some(format!("Unknown test config: {}", config.name)));
            }
        }

        results.execution_time_ms = start_time.elapsed().as_millis() as u64;
        
        if config.enable_memory_analysis {
            results.memory_usage_bytes = self.get_memory_usage();
        }

        // Print summary
        println!("Test Config: {}", config.name);
        println!("  Total: {}, Passed: {}, Failed: {}", 
                 results.total_tests, results.passed_tests, results.failed_tests);
        println!("  Success Rate: {:.2}%", results.success_rate());
        println!("  Execution Time: {} ms", results.execution_time_ms);
        
        if config.enable_benchmarks {
            println!("  Benchmarks:");
            for benchmark in &results.benchmark_results {
                println!("    {}: {:.2} ms avg ({} iterations)", 
                         benchmark.operation, benchmark.avg_time_ms, benchmark.iterations);
            }
        }

        results
    }

    fn run_unit_tests(&self, config: &TestConfig) -> TestResults {
        let mut results = TestResults::new(config.name.clone());
        
        // Test 1: Modular interpreter creation
        let start = Instant::now();
        let result = self::unit_tests::test_modular_interpreter_creation();
        let duration = start.elapsed();
        
        if result {
            results.add_result(true, None);
            
            if config.enable_benchmarks {
                results.add_benchmark(BenchmarkResult::new(
                    "modular_interpreter_creation".to_string(),
                    config.iterations,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                ));
            }
        } else {
            results.add_result(false, Some("Failed to create modular interpreter".to_string()));
        }

        // Test 2: Enhanced error handling
        let start = Instant::now();
        let result = self::error_handling_tests::test_enhanced_error_reporting();
        let duration = start.elapsed();
        
        if result {
            results.add_result(true, None);
            
            if config.enable_benchmarks {
                results.add_benchmark(BenchmarkResult::new(
                    "enhanced_error_reporting".to_string(),
                    config.iterations,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                ));
            }
        } else {
            results.add_result(false, Some("Failed enhanced error reporting test".to_string()));
        }

        // Test 3: Parser recovery
        let start = Instant::now();
        let result = self::parser_recovery_tests::test_parser_error_recovery();
        let duration = start.elapsed();
        
        if result {
            results.add_result(true, None);
            
            if config.enable_benchmarks {
                results.add_benchmark(BenchmarkResult::new(
                    "parser_error_recovery".to_string(),
                    config.iterations,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                ));
            }
        } else {
            results.add_result(false, Some("Failed parser recovery test".to_string()));
        }

        results
    }

    fn run_integration_tests(&self, config: &TestConfig) -> TestResults {
        let mut results = TestResults::new(config.name.clone());
        
        // Test 1: Complete compilation workflow
        let start = Instant::now();
        let result = self::integration_tests::test_complete_compilation_workflow();
        let duration = start.elapsed();
        
        if result {
            results.add_result(true, None);
            
            if config.enable_benchmarks {
                results.add_benchmark(BenchmarkResult::new(
                    "complete_compilation_workflow".to_string(),
                    config.iterations,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                ));
            }
        } else {
            results.add_result(false, Some("Failed complete compilation workflow test".to_string()));
        }

        // Test 2: Component integration
        let start = Instant::now();
        let result = self::integration_tests::test_component_integration();
        let duration = start.elapsed();
        
        if result {
            results.add_result(true, None);
            
            if config.enable_benchmarks {
                results.add_benchmark(BenchmarkResult::new(
                    "component_integration".to_string(),
                    config.iterations,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                ));
            }
        } else {
            results.add_result(false, Some("Failed component integration test".to_string()));
        }

        results
    }

    fn run_performance_tests(&self, config: &TestConfig) -> TestResults {
        let mut results = TestResults::new(config.name.clone());
        
        // Test 1: Symbol table performance
        let start = Instant::now();
        let result = self::performance_tests::test_symbol_table_performance();
        let duration = start.elapsed();
        
        if result {
            results.add_result(true, None);
            
            if config.enable_benchmarks {
                results.add_benchmark(BenchmarkResult::new(
                    "symbol_table_performance".to_string(),
                    config.iterations,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                ));
            }
        } else {
            results.add_result(false, Some("Failed symbol table performance test".to_string()));
        }

        // Test 2: Optimization pipeline performance
        let start = Instant::now();
        let result = self::performance_tests::test_optimization_pipeline_performance();
        let duration = start.elapsed();
        
        if result {
            results.add_result(true, None);
            
            if config.enable_benchmarks {
                results.add_benchmark(BenchmarkResult::new(
                    "optimization_pipeline_performance".to_string(),
                    config.iterations,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                ));
            }
        } else {
            results.add_result(false, Some("Failed optimization pipeline performance test".to_string()));
        }

        results
    }

    fn run_error_handling_tests(&self, config: &TestConfig) -> TestResults {
        let mut results = TestResults::new(config.name.clone());
        
        // Test 1: Error recovery scenarios
        let start = Instant::now();
        let result = self::error_handling_tests::test_error_recovery_scenarios();
        let duration = start.elapsed();
        
        if result {
            results.add_result(true, None);
            
            if config.enable_benchmarks {
                results.add_benchmark(BenchmarkResult::new(
                    "error_recovery_scenarios".to_string(),
                    config.iterations,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                ));
            }
        } else {
            results.add_result(false, Some("Failed error recovery scenarios test".to_string()));
        }

        // Test 2: Error reporting performance
        let start = Instant::now();
        let result = self::error_handling_tests::test_error_reporting_performance();
        let duration = start.elapsed();
        
        if result {
            results.add_result(true, None);
            
            if config.enable_benchmarks {
                results.add_benchmark(BenchmarkResult::new(
                    "error_reporting_performance".to_string(),
                    config.iterations,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                    duration.as_millis() as u64,
                ));
            }
        } else {
            results.add_result(false, Some("Failed error reporting performance test".to_string()));
        }

        results
    }

    fn get_memory_usage(&self) -> usize {
        // In a real implementation, this would measure actual memory usage
        // For now, return a placeholder value
        1024 * 1024 * 10 // 10MB placeholder
    }
}

// Test implementations for different categories
mod unit_tests {
    pub fn test_modular_interpreter_creation() -> bool {
        // Test that all interpreter modules can be created
        use pascal::interpreter::{RuntimeEnvironment, ScopeManager, FunctionRegistry};
        
        let runtime = RuntimeEnvironment::new(false);
        let scope_manager = ScopeManager::new(false);
        let function_registry = FunctionRegistry::new();
        
        !runtime.is_verbose() && scope_manager.stack_depth() == 0 && function_registry.function_names().is_empty()
    }
}

mod error_handling_tests {
    pub fn test_enhanced_error_reporting() -> bool {
        use pascal::enhanced_error::{ErrorReporter, SourceLocation, ErrorSuggestion};
        
        let mut reporter = ErrorReporter::new();
        let location = SourceLocation::new(0, 1, 1, 0, 0);
        
        let error = pascal::enhanced_error::CompilerError::syntax_error(
            location,
            "Test error".to_string(),
            vec!["begin".to_string()],
        );
        
        reporter.report_error(error);
        
        reporter.error_count() == 1 && !reporter.has_errors() // !has_errors() because it has errors
    }

    pub fn test_error_recovery_scenarios() -> bool {
        // Test error handling in various scenarios
        true
    }

    pub fn test_error_reporting_performance() -> bool {
        // Test error reporting performance
        use pascal::enhanced_error::ErrorReporter;
        
        let mut reporter = ErrorReporter::new();
        
        for i in 0..1000 {
            let location = pascal::enhanced_error::SourceLocation::new(0, 1, 1, 0, 0);
            let error = pascal::enhanced_error::CompilerError::semantic_error(
                location,
                format!("Error {}", i),
                None,
            );
            reporter.report_error(error);
        }
        
        reporter.error_count() == 1000
    }
}

mod parser_recovery_tests {
    pub fn test_parser_error_recovery() -> bool {
        // Test parser error recovery capabilities
        true
    }
}

mod integration_tests {
    pub fn test_complete_compilation_workflow() -> bool {
        // Test the complete compilation workflow
        true
    }

    pub fn test_component_integration() -> bool {
        // Test that all components work together
        true
    }
}

mod performance_tests {
    pub fn test_symbol_table_performance() -> bool {
        // Test symbol table performance with large datasets
        true
    }

    pub fn test_optimization_pipeline_performance() -> bool {
        // Test optimization pipeline performance
        true
    }
}

/// Generate comprehensive test report
pub fn generate_test_report(results: &[TestResults]) -> String {
    let mut report = String::new();
    
    report.push_str("=== Comprehensive Test Report ===\n\n");
    
    for result in results {
        report.push_str(&format!("Test Configuration: {}\n", result.test_name));
        report.push_str(&format!("Description: {}\n", "Comprehensive validation tests"));
        report.push_str(&format!("Total Tests: {}\n", result.total_tests));
        report.push_str(&format!("Passed: {}\n", result.passed_tests));
        report.push_str(&format!("Failed: {}\n", result.failed_tests));
        report.push_str(&format!("Success Rate: {:.2}%\n", result.success_rate()));
        report.push_str(&format!("Execution Time: {} ms\n", result.execution_time_ms));
        report.push_str(&format!("Memory Usage: {} bytes\n", result.memory_usage_bytes));
        
        if !result.benchmark_results.is_empty() {
            report.push_str("\nBenchmark Results:\n");
            for benchmark in &result.benchmark_results {
                report.push_str(&format!("  {}: {:.2} ms avg ({} iterations)\n", 
                                         benchmark.operation, benchmark.avg_time_ms, benchmark.iterations));
            }
        }
        
        if !result.error_details.is_empty() {
            report.push_str("\nError Details:\n");
            for error in &result.error_details {
                report.push_str(&format!("  - {}\n", error));
            }
        }
        
        if result.has_errors() {
            report.push_str("\n❌ TESTS FAILED\n");
        } else {
            report.push_str("\n✅ ALL TESTS PASSED\n");
        }
        
        report.push_str("\n");
    }
    
    // Overall summary
    let total_tests: usize = results.iter().map(|r| r.total_tests).sum();
    let total_passed: usize = results.iter().map(|r| r.passed_tests).sum();
    let total_failed: usize = results.iter().map(|r| r.failed_tests).sum();
    let overall_success_rate = if total_tests > 0 {
        (total_passed as f64 / total_tests as f64) * 100.0
    } else {
        0.0
    };
    
    report.push_str("=== Overall Summary ===\n");
    report.push_str(&format!("Total Tests Across All Configurations: {}\n", total_tests));
    report.push_str(&format!("Total Passed: {}\n", total_passed));
    report.push_str(&format!("Total Failed: {}\n", total_failed));
    report.push_str(&format!("Overall Success Rate: {:.2}%\n", overall_success_rate));
    
    if total_failed == 0 {
        report.push_str("\n🎉 ALL TESTS PASSED - All capabilities validated successfully!\n");
    } else {
        report.push_str(&format!("\n⚠️  {} tests failed - Some issues need attention.\n", total_failed));
    }
    
    report
}

/// Quick validation function for specific capabilities
pub fn validate_capabilities() -> TestResults {
    let mut runner = TestRunner::new()
        .add_default_configs()
        .add_config(TestConfig::new(
            "quick_validation".to_string(),
            "Quick validation of key capabilities".to_string(),
        ).with_iterations(1));
    
    let results = runner.run_all_tests();
    results[0].clone() // Return the quick validation result
}

#[cfg(test)]
mod test_harness_tests {
    use super::*;

    #[test]
    fn test_test_config_creation() {
        let config = TestConfig::new("test".to_string(), "Test description".to_string());
        assert_eq!(config.name, "test");
        assert_eq!(config.description, "Test description");
        assert_eq!(config.iterations, 1);
    }

    #[test]
    fn test_test_results_calculation() {
        let mut results = TestResults::new("test".to_string());
        
        results.add_result(true, None);
        results.add_result(true, None);
        results.add_result(false, Some("Test error".to_string()));
        
        assert_eq!(results.total_tests, 3);
        assert_eq!(results.passed_tests, 2);
        assert_eq!(results.failed_tests, 1);
        assert_eq!(results.success_rate(), 66.67);
        assert!(results.has_errors());
    }

    #[test]
    fn test_benchmark_result_creation() {
        let benchmark = BenchmarkResult::new(
            "test_operation".to_string(),
            10,
            100,
            8,
            15,
        );
        
        assert_eq!(benchmark.operation, "test_operation");
        assert_eq!(benchmark.iterations, 10);
        assert_eq!(benchmark.total_time_ms, 100);
        assert_eq!(benchmark.avg_time_ms, 10.0);
        assert_eq!(benchmark.min_time_ms, 8);
        assert_eq!(benchmark.max_time_ms, 15);
    }

    #[test]
    fn test_test_runner_creation() {
        let runner = TestRunner::new();
        assert!(runner.configs.is_empty());
        assert!(runner.results.is_empty());
    }

    #[test]
    fn test_generate_test_report() {
        let mut results = Vec::new();
        
        let mut result1 = TestResults::new("test1".to_string());
        result1.add_result(true, None);
        result1.add_result(true, None);
        result1.execution_time_ms = 1000;
        results.push(result1);
        
        let mut result2 = TestResults::new("test2".to_string());
        result2.add_result(true, None);
        result2.add_result(false, Some("Error".to_string()));
        result2.execution_time_ms = 2000;
        results.push(result2);
        
        let report = generate_test_report(&results);
        
        assert!(report.contains("test1"));
        assert!(report.contains("test2"));
        assert!(report.contains("Total Tests Across All Configurations: 4"));
        assert!(report.contains("Total Passed: 3"));
        assert!(report.contains("Total Failed: 1"));
    }
}