pascal 0.1.4

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
//! Comprehensive threading tests for pascal-rs compiler
//! Tests parallel compilation, thread safety, and performance

use pascal::{ParallelConfig, ParallelCompiler, ProgressTracker, Module, ModuleError, ModuleResult};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::thread;

// Helper function to create a mock module
fn create_mock_module(name: &str) -> Module {
    Module {
        name: name.to_string(),
        // Add other required fields
        imports: Vec::new(),
        exports: Vec::new(),
        statements: Vec::new(),
        functions: Vec::new(),
        procedures: Vec::new(),
    }
}

// Helper function to simulate module compilation
fn compile_module_mock(name: &str) -> ModuleResult<Module> {
    // Simulate some work
    std::thread::sleep(Duration::from_millis(10));
    Ok(create_mock_module(name))
}

// Helper function to simulate failing compilation
fn compile_module_failing(name: &str) -> ModuleResult<Module> {
    if name == "fail" {
        Err(ModuleError::CompilationError {
            message: "Intentional failure".to_string(),
        })
    } else {
        compile_module_mock(name)
    }
}

#[test]
fn test_parallel_config_default() {
    let config = ParallelConfig::new();

    assert_eq!(config.num_threads, 0, "Default should be auto-detect");
    assert!(config.parallel_modules, "Parallel modules should be enabled by default");
    assert!(
        config.parallel_optimization,
        "Parallel optimization should be enabled by default"
    );
    assert_eq!(config.min_modules_for_parallel, 2, "Default min modules should be 2");
}

#[test]
fn test_parallel_config_with_threads() {
    let config = ParallelConfig::new().with_threads(4);

    assert_eq!(config.num_threads, 4);
}

#[test]
fn test_parallel_config_with_parallel_modules() {
    let config = ParallelConfig::new().with_parallel_modules(false);

    assert!(!config.parallel_modules);
}

#[test]
fn test_parallel_config_with_parallel_optimization() {
    let config = ParallelConfig::new().with_parallel_optimization(false);

    assert!(!config.parallel_optimization);
}

#[test]
fn test_parallel_config_builder_chain() {
    let config = ParallelConfig::new()
        .with_threads(8)
        .with_parallel_modules(true)
        .with_parallel_optimization(true);

    assert_eq!(config.num_threads, 8);
    assert!(config.parallel_modules);
    assert!(config.parallel_optimization);
}

#[test]
fn test_parallel_compiler_creation() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    // Just verify it's created successfully
    assert_eq!(compiler.config.num_threads, 0);
}

#[test]
fn test_parallel_compiler_with_config() {
    let config = ParallelConfig::new().with_threads(4);
    let compiler = ParallelCompiler::new(config);

    assert_eq!(compiler.config.num_threads, 4);
}

#[test]
fn test_progress_tracker_new() {
    let tracker = ProgressTracker::new(10);

    assert_eq!(tracker.completed(), 0);
    assert_eq!(tracker.total(), 10);
    assert_eq!(tracker.progress(), 0.0);
    assert!(tracker.errors().is_empty());
    assert!(!tracker.is_complete());
}

#[test]
fn test_progress_tracker_complete_one() {
    let tracker = ProgressTracker::new(10);

    tracker.complete_one();

    assert_eq!(tracker.completed(), 1);
    assert!((tracker.progress() - 0.1).abs() < 0.001);
    assert!(!tracker.is_complete());
}

#[test]
fn test_progress_tracker_complete_all() {
    let tracker = ProgressTracker::new(10);

    for _ in 0..10 {
        tracker.complete_one();
    }

    assert_eq!(tracker.completed(), 10);
    assert_eq!(tracker.progress(), 1.0);
    assert!(tracker.is_complete());
}

#[test]
fn test_progress_tracker_add_error() {
    let tracker = ProgressTracker::new(10);

    tracker.add_error("Error 1".to_string());
    tracker.add_error("Error 2".to_string());

    assert_eq!(tracker.errors().len(), 2);
    assert_eq!(tracker.errors()[0], "Error 1");
    assert_eq!(tracker.errors()[1], "Error 2");
}

#[test]
fn test_progress_tracker_with_completion_and_errors() {
    let tracker = ProgressTracker::new(10);

    // Complete 8 tasks
    for _ in 0..8 {
        tracker.complete_one();
    }

    // Add 2 errors
    tracker.add_error("Error 1".to_string());
    tracker.add_error("Error 2".to_string());

    assert_eq!(tracker.completed(), 8);
    assert_eq!(tracker.errors().len(), 2);
    assert!((tracker.progress() - 0.8).abs() < 0.001);
    assert!(!tracker.is_complete());
}

#[test]
fn test_progress_tracker_clone() {
    let tracker = Arc::new(ProgressTracker::new(10));

    // Simulate multiple threads updating progress
    let handles: Vec<_> = (0..5)
        .map(|_| {
            let tracker = Arc::clone(&tracker);
            thread::spawn(move || {
                for _ in 0..2 {
                    tracker.complete_one();
                    thread::sleep(Duration::from_millis(1));
                }
            })
        })
        .collect();

    // Wait for all threads
    for handle in handles {
        handle.join().unwrap();
    }

    assert_eq!(tracker.completed(), 10);
    assert!(tracker.is_complete());
}

#[test]
fn test_parallel_compilation_empty_modules() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    let modules = Vec::new();
    let results = compiler.compile_modules_parallel(modules, compile_module_mock);

    assert!(results.is_empty());
}

#[test]
fn test_parallel_compilation_single_module() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    let modules = vec!["module1".to_string()];
    let results = compiler.compile_modules_parallel(modules, compile_module_mock);

    assert_eq!(results.len(), 1);
    assert!(results[0].is_ok());
    assert_eq!(results[0].as_ref().unwrap().name, "module1");
}

#[test]
fn test_parallel_compilation_multiple_modules() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    let modules = vec![
        "module1".to_string(),
        "module2".to_string(),
        "module3".to_string(),
    ];
    let results = compiler.compile_modules_parallel(modules, compile_module_mock);

    assert_eq!(results.len(), 3);
    assert!(results[0].is_ok());
    assert!(results[1].is_ok());
    assert!(results[2].is_ok());
}

#[test]
fn test_parallel_compilation_with_errors() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    let modules = vec![
        "module1".to_string(),
        "fail".to_string(),
        "module3".to_string(),
    ];
    let results = compiler.compile_modules_parallel(modules, compile_module_failing);

    assert_eq!(results.len(), 3);
    assert!(results[0].is_ok());
    assert!(results[1].is_err());
    assert!(results[2].is_ok());
}

#[test]
fn test_parallel_compilation_sequential_below_threshold() {
    let config = ParallelConfig::new()
        .with_parallel_modules(true)
        .with_min_modules_for_parallel(5);
    let compiler = ParallelCompiler::new(config);

    let modules = vec!["module1".to_string(), "module2".to_string()];
    let results = compiler.compile_modules_parallel(modules, compile_module_mock);

    // Should still work, just sequentially
    assert_eq!(results.len(), 2);
    assert!(results[0].is_ok());
    assert!(results[1].is_ok());
}

#[test]
fn test_parallel_optimization_empty() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    let items: Vec<i32> = Vec::new();
    let results = compiler.optimize_parallel(items, |x| x * 2);

    assert!(results.is_empty());
}

#[test]
fn test_parallel_optimization_single_item() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    let items = vec![1];
    let results = compiler.optimize_parallel(items, |x| x * 2);

    assert_eq!(results.len(), 1);
    assert_eq!(results[0], 2);
}

#[test]
fn test_parallel_optimization_multiple_items() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    let items = vec![1, 2, 3, 4, 5];
    let results = compiler.optimize_parallel(items, |x| x * 2);

    assert_eq!(results.len(), 5);
    assert_eq!(results, vec![2, 4, 6, 8, 10]);
}

#[test]
fn test_parallel_optimization_with_complex_function() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    let items = vec![1, 2, 3, 4, 5];
    let results = compiler.optimize_parallel(items, |x| {
        // Simulate some work
        let mut result = x;
        for _ in 0..10 {
            result = result * 2 + 1;
        }
        result
    });

    assert_eq!(results.len(), 5);
}

#[test]
fn test_parallel_config_init_thread_pool_default() {
    let config = ParallelConfig::new();

    let result = config.init_thread_pool();
    assert!(result.is_ok());
}

#[test]
fn test_parallel_config_init_thread_pool_custom() {
    let config = ParallelConfig::new().with_threads(4);

    let result = config.init_thread_pool();
    // Might fail if thread pool already initialized
    // Just check it returns a Result
    let _ = result;
}

#[test]
fn test_parallel_compilation_thread_safety() {
    let config = ParallelConfig::new().with_threads(4);
    let compiler = ParallelCompiler::new(config);
    let tracker = Arc::new(ProgressTracker::new(100));

    let modules: Vec<String> = (0..100).map(|i| format!("module{}", i)).collect();
    let tracker_clone = Arc::clone(&tracker);

    let results = compiler.compile_modules_parallel(modules, |name| {
        tracker_clone.complete_one();
        compile_module_mock(name)
    });

    assert_eq!(results.len(), 100);
    assert!(tracker.is_complete());
}

#[test]
fn test_parallel_compilation_with_state() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    // Use a counter to verify all modules were processed
    let counter = Arc::new(Mutex::new(0));
    let modules: Vec<String> = (0..10).map(|i| format!("module{}", i)).collect();

    let counter_clone = Arc::clone(&counter);
    let results = compiler.compile_modules_parallel(modules, |name| {
        let mut count = counter_clone.lock().unwrap();
        *count += 1;
        drop(count);
        compile_module_mock(name)
    });

    assert_eq!(results.len(), 10);
    assert_eq!(*counter.lock().unwrap(), 10);
}

#[test]
fn test_parallel_compilation_ordering() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    let modules = vec![
        "module1".to_string(),
        "module2".to_string(),
        "module3".to_string(),
    ];

    let results = compiler.compile_modules_parallel(modules, compile_module_mock);

    // Results should be in the same order as input
    assert_eq!(results.len(), 3);
    assert_eq!(results[0].as_ref().unwrap().name, "module1");
    assert_eq!(results[1].as_ref().unwrap().name, "module2");
    assert_eq!(results[2].as_ref().unwrap().name, "module3");
}

#[test]
fn test_parallel_compilation_disabled() {
    let config = ParallelConfig::new().with_parallel_modules(false);
    let compiler = ParallelCompiler::new(config);

    let modules = vec![
        "module1".to_string(),
        "module2".to_string(),
        "module3".to_string(),
    ];

    let results = compiler.compile_modules_parallel(modules, compile_module_mock);

    // Should still work, just sequentially
    assert_eq!(results.len(), 3);
    assert!(results[0].is_ok());
    assert!(results[1].is_ok());
    assert!(results[2].is_ok());
}

#[test]
fn test_parallel_optimization_disabled() {
    let config = ParallelConfig::new().with_parallel_optimization(false);
    let compiler = ParallelCompiler::new(config);

    let items = vec![1, 2, 3, 4, 5];
    let results = compiler.optimize_parallel(items, |x| x * 2);

    // Should still work
    assert_eq!(results.len(), 5);
}

#[test]
fn test_progress_tracker_thread_safety_concurrent_updates() {
    let tracker = Arc::new(ProgressTracker::new(1000));
    let handles: Vec<_> = (0..10)
        .map(|_| {
            let tracker = Arc::clone(&tracker);
            thread::spawn(move || {
                for _ in 0..100 {
                    tracker.complete_one();
                    tracker.add_error(format!("Error from thread"));
                }
            })
        })
        .collect();

    for handle in handles {
        handle.join().unwrap();
    }

    assert_eq!(tracker.completed(), 1000);
    assert_eq!(tracker.errors().len(), 1000);
    assert!(tracker.is_complete());
}

#[test]
fn test_progress_tracker_zero_total() {
    let tracker = ProgressTracker::new(0);

    assert_eq!(tracker.total(), 0);
    assert_eq!(tracker.completed(), 0);
    assert!(tracker.is_complete());
}

#[test]
fn test_progress_tracker_fractional_progress() {
    let tracker = ProgressTracker::new(3);

    tracker.complete_one();

    assert!((tracker.progress() - 0.333).abs() < 0.01);

    tracker.complete_one();

    assert!((tracker.progress() - 0.666).abs() < 0.01);
}

#[test]
fn test_parallel_compilation_large_number_of_modules() {
    let config = ParallelConfig::new().with_threads(8);
    let compiler = ParallelCompiler::new(config);

    let modules: Vec<String> = (0..1000).map(|i| format!("module{}", i)).collect();
    let results = compiler.compile_modules_parallel(modules, compile_module_mock);

    assert_eq!(results.len(), 1000);

    // Verify all succeeded
    for result in &results {
        assert!(result.is_ok());
    }
}

#[test]
fn test_parallel_optimization_with_side_effects() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    let items = vec![1, 2, 3, 4, 5];
    let results = compiler.optimize_parallel(items, |x| {
        // Simulate optimization pass
        if x % 2 == 0 {
            x * 2
        } else {
            x + 1
        }
    });

    assert_eq!(results.len(), 5);
    assert_eq!(results, vec![2, 4, 4, 8, 6]);
}

#[test]
fn test_parallel_config_min_modules_threshold() {
    let config = ParallelConfig::new()
        .with_parallel_modules(true)
        .with_min_modules_for_parallel(3);
    let compiler = ParallelCompiler::new(config);

    // Below threshold - should work sequentially
    let modules = vec!["module1".to_string(), "module2".to_string()];
    let results = compiler.compile_modules_parallel(modules, compile_module_mock);

    assert_eq!(results.len(), 2);

    // At threshold - should work in parallel
    let modules = vec![
        "module1".to_string(),
        "module2".to_string(),
        "module3".to_string(),
    ];
    let results = compiler.compile_modules_parallel(modules, compile_module_mock);

    assert_eq!(results.len(), 3);
}

#[test]
fn test_parallel_compilation_all_failures() {
    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    let modules = vec!["fail1".to_string(), "fail2".to_string(), "fail3".to_string()];

    let results = compiler.compile_modules_parallel(modules, |name| {
        Err(ModuleError::CompilationError {
            message: format!("Failed: {}", name),
        })
    });

    assert_eq!(results.len(), 3);
    assert!(results[0].is_err());
    assert!(results[1].is_err());
    assert!(results[2].is_err());
}

#[test]
fn test_progress_tracker_does_not_panic_on_over_completion() {
    let tracker = ProgressTracker::new(5);

    // Complete more than total
    for _ in 0..10 {
        tracker.complete_one();
    }

    // Should handle gracefully
    assert_eq!(tracker.completed(), 10);
    assert!(tracker.progress() > 1.0);
}

#[test]
fn test_parallel_config_debug_format() {
    let config = ParallelConfig::new()
        .with_threads(4)
        .with_parallel_modules(true)
        .with_parallel_optimization(false);

    // Just verify it can be formatted
    let debug_str = format!("{:?}", config);
    assert!(debug_str.contains("ParallelConfig"));
}

#[test]
fn test_parallel_compiler_send_sync() {
    // Verify ParallelCompiler implements Send and Sync
    fn assert_send_sync<T: Send + Sync>() {}

    assert_send_sync::<ParallelCompiler>();

    let config = ParallelConfig::new();
    let compiler = ParallelCompiler::new(config);

    // Verify it can be sent between threads
    let handle = thread::spawn(move || {
        // Use compiler in different thread
        let modules = vec!["test".to_string()];
        let _ = compiler.compile_modules_parallel(modules, compile_module_mock);
    });

    handle.join().unwrap();
}

#[test]
fn test_progress_tracker_send_sync() {
    // Verify ProgressTracker implements Send and Sync
    fn assert_send_sync<T: Send + Sync>() {}

    assert_send_sync::<ProgressTracker>();

    let tracker = Arc::new(ProgressTracker::new(10));

    let handle = thread::spawn(move || {
        tracker.complete_one();
        tracker.completed()
    });

    let completed = handle.join().unwrap();
    assert_eq!(completed, 1);
}