xdy 0.13.0

Complex RPG dice expression evaluator with histogram support.
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
//! # Histogram test cases
//!
//! Herein are tests for the histogram builder. Actual test cases are stored in
//! in `../../tests/test_histograms.txt`, which comprises a series of test
//! cases, each of which consists of a source dice expression and an expected
//! print rendition.

use std::collections::{BTreeMap, HashSet};
#[cfg(feature = "parallel-histogram")]
use std::{
	panic::{self, AssertUnwindSafe},
	sync::atomic::{AtomicBool, Ordering}
};

use pretty_assertions::assert_eq;
#[cfg(feature = "parallel-histogram")]
use rayon::iter::ParallelIterator;

#[cfg(feature = "parallel-histogram")]
use crate::support::on_small_stack;
use crate::{
	EvaluationError, Evaluator, HistogramBuilder, Passes, serial,
	support::{compile_valid, optimize, read_histogram_test_cases}
};

////////////////////////////////////////////////////////////////////////////////
//                             Histogram support.                             //
////////////////////////////////////////////////////////////////////////////////

/// Run a set of histogram test cases.
///
/// # Parameters
/// - `source`: The contents of a test case file.
/// - `builder`: A function that constructs a histogram builder from an
///   evaluator.
pub fn histogram_test<I, T, B>(source: &'static str, builder: B)
where
	I: 'static,
	T: HistogramBuilder<'static, I>,
	B: Fn(Evaluator) -> T
{
	let mut seen = HashSet::new();
	for (index, (source, args, externs, expected)) in
		read_histogram_test_cases(source).iter().enumerate()
	{
		let key = (source, args.clone(), externs.clone());
		let key = format!("{:?}", key);
		assert!(seen.insert(key.clone()), "duplicate test case: {}", key);
		let function = compile_valid(source);
		let function = optimize(function, Passes::all());
		let mut evaluator = Evaluator::new(function);
		for (name, value) in externs.iter()
		{
			evaluator.bind(name, *value).unwrap();
		}
		let bounds = evaluator
			.bounds_over(
				args.iter().map(|arg| Some((*arg).into())),
				externs.iter().map(|(name, value)| (*name, (*value).into()))
			)
			.unwrap();
		// Ensure that the number of outcomes is reasonable for testing.
		assert!(
			bounds.count.map(|c| c <= 50000).unwrap_or(true),
			"case {}: {}: too many outcomes: {} > 50000",
			index + 1,
			key,
			bounds.count.unwrap()
		);
		// Set up the map of expected results.
		let expected = expected
			.iter()
			.map(|(key, value)| (*key, *value as u64))
			.collect::<BTreeMap<_, _>>();
		// Build the histogram.
		let builder = builder(evaluator);
		let histogram = builder.build(args.iter().copied()).unwrap();
		{
			let histogram_map = histogram
				.iter()
				.map(|(outcome, count)| (*outcome, *count))
				.collect::<BTreeMap<_, _>>();
			assert_eq!(histogram_map, expected, "case {}: {}", index + 1, key);
		}
		// Ensure that the histogram bounds are correct.
		let value_bounds = bounds.value;
		assert_eq!(
			*histogram.keys().min().unwrap(),
			value_bounds.min,
			"case {}: {}: min value bound mismatch",
			index + 1,
			key
		);
		assert_eq!(
			*histogram.keys().max().unwrap(),
			value_bounds.max,
			"case {}: {}: max value bound mismatch",
			index + 1,
			key
		);
		// Ensure that the outcome counts are correct.
		if let Some(expected_outcomes) = bounds.count
		{
			let actual_outcomes: u128 =
				histogram.values().map(|count| *count as u128).sum();
			assert_eq!(
				actual_outcomes,
				expected_outcomes,
				"case {}: {}: outcome count mismatch",
				index + 1,
				key
			);
		}
		// Ensure that the odds are correct.
		let total = histogram.total();
		for (outcome, count) in histogram.iter()
		{
			let odds = histogram.odds(*outcome);
			let expected_odds = (*count, total - *count);
			assert_eq!(
				odds,
				expected_odds,
				"case {}: {}: odds mismatch for outcome {}",
				index + 1,
				key,
				outcome
			);
			let percent = histogram.percent_chance(*outcome);
			let expected_percent = (*count as f64 / total as f64) * 100.0;
			assert_eq!(
				percent,
				expected_percent,
				"case {}: {}: percent mismatch for outcome {}",
				index + 1,
				key,
				outcome
			);
		}
	}
}

////////////////////////////////////////////////////////////////////////////////
//                              Histogram tests.                              //
////////////////////////////////////////////////////////////////////////////////

/// The content of the test file for histograms.
const HISTOGRAM_TEST_SOURCE: &str =
	include_str!("../../tests/test_histograms.txt");

/// Test that histograms are built correctly by the serial builder.
#[test]
fn test_serial_histogram_building()
{
	histogram_test(HISTOGRAM_TEST_SOURCE, serial::HistogramBuilder::new)
}

/// Test that histograms are built correctly by the parallel builder.
#[cfg(feature = "parallel-histogram")]
#[test]
fn test_parallel_histogram_building()
{
	histogram_test(
		HISTOGRAM_TEST_SOURCE,
		crate::parallel::HistogramBuilder::new
	)
}

////////////////////////////////////////////////////////////////////////////////
//                           Deep histogram tests.                            //
////////////////////////////////////////////////////////////////////////////////

/// The stack size of the worker threads in the deep histogram tests: the
/// default stack size of a `rayon` worker.
#[cfg(feature = "parallel-histogram")]
const WORKER_STACK_SIZE: usize = 2 * 1024 * 1024;

/// Ensure that the parallel builder explores the deep state space of many dice
/// on a single worker with a small stack.
#[cfg(feature = "parallel-histogram")]
#[test]
fn test_parallel_histogram_many_dice_one_thread()
{
	on_small_stack(|| build_many_dice(1))
}

/// Ensure that the parallel builder explores the deep state space of many dice
/// on several workers with small stacks.
#[cfg(feature = "parallel-histogram")]
#[test]
fn test_parallel_histogram_many_dice_four_threads()
{
	on_small_stack(|| build_many_dice(4))
}

/// Ensure that the parallel builder explores the deep state space of dice
/// with many faces on a single worker with a small stack, and that it agrees
/// with the serial builder.
#[cfg(feature = "parallel-histogram")]
#[test]
fn test_parallel_histogram_many_faces_one_thread()
{
	on_small_stack(|| build_many_faces(1))
}

/// Ensure that the parallel builder explores the deep state space of dice
/// with many faces on several workers with small stacks, and that it agrees
/// with the serial builder.
#[cfg(feature = "parallel-histogram")]
#[test]
fn test_parallel_histogram_many_faces_four_threads()
{
	on_small_stack(|| build_many_faces(4))
}

/// Ensure that a panic in the consumer of the parallel builder's iterator
/// propagates to the caller, rather than stranding the other workers.
#[cfg(feature = "parallel-histogram")]
#[test]
fn test_parallel_histogram_consumer_panic()
{
	on_small_stack(|| {
		// Panic only once, so that the other workers survive and must finish
		// the exploration without the panicking worker.
		let builder = parallel_builder("6D6");
		let panicked = AtomicBool::new(false);
		let outcome = panic::catch_unwind(AssertUnwindSafe(|| {
			in_pool(4, || {
				builder.iter([]).unwrap().for_each(|state| {
					if state.result == Some(21)
						&& !panicked.swap(true, Ordering::Relaxed)
					{
						panic!("deliberate panic in the consumer")
					}
				})
			})
		}));
		assert!(outcome.is_err());
	})
}

/// Build a partial histogram of `800D6`, whose state space is 800 dice deep,
/// with the parallel builder, and check that it contains exactly the requested
/// number of plausible outcomes.
///
/// # Parameters
/// - `threads`: The number of worker threads.
#[cfg(feature = "parallel-histogram")]
fn build_many_dice(threads: usize)
{
	const LIMIT: u64 = 1000;
	let builder = parallel_builder("800D6");
	let histogram =
		in_pool(threads, || builder.build_with_limit([], LIMIT).unwrap());
	assert_eq!(histogram.total(), LIMIT);
	assert!(
		histogram
			.keys()
			.all(|outcome| (800..=4800).contains(outcome)),
		"implausible outcome: {:?}",
		histogram.iter().collect::<BTreeMap<_, _>>()
	);
}

/// Build the complete histogram of `2D1000`, which drove the recursive
/// parallel builder about a thousand levels deep, with the parallel builder,
/// and check that it agrees with the serial builder.
///
/// # Parameters
/// - `threads`: The number of worker threads.
#[cfg(feature = "parallel-histogram")]
fn build_many_faces(threads: usize)
{
	const SOURCE: &str = "2D1000";
	let builder = parallel_builder(SOURCE);
	let parallel = in_pool(threads, || builder.build([]).unwrap());
	let serial =
		serial::HistogramBuilder::new(Evaluator::new(compile_valid(SOURCE)))
			.build([])
			.unwrap();
	assert_eq!(parallel.total(), 1_000_000);
	assert_eq!(
		parallel.iter().collect::<BTreeMap<_, _>>(),
		serial.iter().collect::<BTreeMap<_, _>>()
	);
}

/// Construct a parallel histogram builder for the specified source.
///
/// # Parameters
/// - `source`: The source of the dice expression, which must be closed.
///
/// # Returns
/// The builder.
#[cfg(feature = "parallel-histogram")]
fn parallel_builder(source: &str) -> crate::parallel::HistogramBuilder
{
	crate::parallel::HistogramBuilder::new(Evaluator::new(compile_valid(
		source
	)))
}

/// Run the specified closure in a dedicated `rayon` thread pool whose workers
/// have [small stacks](WORKER_STACK_SIZE).
///
/// # Parameters
/// - `threads`: The number of worker threads.
/// - `f`: The closure to run.
///
/// # Returns
/// The result of the closure.
#[cfg(feature = "parallel-histogram")]
fn in_pool<R: Send>(threads: usize, f: impl FnOnce() -> R + Send) -> R
{
	rayon::ThreadPoolBuilder::new()
		.num_threads(threads)
		.stack_size(WORKER_STACK_SIZE)
		.build()
		.unwrap()
		.install(f)
}

/// Test that the histogram builder takes the [maximum](crate::Max), which only
/// the optimizer emits, of every outcome.
#[test]
fn test_histogram_max()
{
	let function = crate::Assembler::assemble(
		"\
Function() r#2 âš…#1
\textern[]
\tbody:
\t\tâš…0 <- roll range 1:6
\t\t@0 <- sum rolling record âš…0
\t\t@1 <- @0 max 3
\t\treturn @1
"
	)
	.unwrap();
	let histogram = serial::HistogramBuilder::new(Evaluator::new(function))
		.build([])
		.unwrap();
	assert_eq!(
		histogram.iter().collect::<BTreeMap<_, _>>(),
		BTreeMap::from([(&3, &3), (&4, &1), (&5, &1), (&6, &1)])
	);
}

////////////////////////////////////////////////////////////////////////////////
//                                 Metering.                                  //
////////////////////////////////////////////////////////////////////////////////

/// Construct a serial histogram builder for the specified source.
///
/// # Parameters
/// - `source`: The source of the dice expression.
///
/// # Returns
/// The builder.
fn serial_builder(source: &str) -> serial::HistogramBuilder
{
	serial::HistogramBuilder::new(Evaluator::new(compile_valid(source)))
}

/// Answer the least budget within which the serial builder builds the
/// histogram of the specified source, i.e., the number of branches that it
/// enumerates, by bisection.
///
/// # Parameters
/// - `source`: The source of the dice expression.
/// - `args`: The arguments.
///
/// # Returns
/// The least sufficient budget.
fn least_budget(source: &str, args: &[i32]) -> u64
{
	let builder = serial_builder(source);
	let (mut low, mut high) = (0, 1u64 << 20);
	assert!(builder.build_metered(args.iter().copied(), high).is_ok());
	while low < high
	{
		let middle = low + (high - low) / 2;
		match builder.build_metered(args.iter().copied(), middle)
		{
			Ok(_) => high = middle,
			Err(EvaluationError::HistogramBudgetExhausted { .. }) =>
			{
				low = middle + 1
			},
			Err(e) => panic!("{}: {}", source, e)
		}
	}
	low
}

/// Test that a metered build charges each range its width and each die its
/// number of faces, once for each path that reaches it, and that a build
/// within budget is exactly the unmetered build.
#[test]
fn test_metered_charges()
{
	for (source, args, branches) in [
		("[1:6]", &[][..], 6),
		("[3:1]", &[], 0),
		("0D6", &[], 0),
		("1D0", &[], 1),
		("1D6", &[], 6),
		("3D6", &[], 6 + 36 + 216),
		("2D[1,2,3]", &[], 3 + 9),
		("{n}: {n}D6", &[2], 42),
		("[1:2] + [1:3]", &[], 2 + 2 * 3),
		("(1D2)D2", &[], 2 + 2 + (2 + 2 * 2))
	]
	{
		assert_eq!(least_budget(source, args), branches, "{}", source);
		let builder = serial_builder(source);
		assert_eq!(
			builder
				.build_metered(args.iter().copied(), branches)
				.unwrap(),
			builder.build(args.iter().copied()).unwrap(),
			"{}",
			source
		);
	}
}

/// Test that a metered build refuses a range or die that exceeds what remains
/// of the budget before enumerating any of it, and reports the refusal
/// exactly.
#[test]
fn test_metered_refusal()
{
	let builder = serial_builder("3D6");
	assert_eq!(
		builder.build_metered([], 257),
		Err(EvaluationError::HistogramBudgetExhausted {
			requested: 6,
			remaining: 5,
			consumed: 252
		})
	);
	assert_eq!(
		builder.build_metered([], 0),
		Err(EvaluationError::HistogramBudgetExhausted {
			requested: 6,
			remaining: 0,
			consumed: 0
		})
	);
}

/// Test that a metered build of a vast state space refuses promptly: `{n}D6`
/// with `n` at [`i32::MAX`], which yields no outcome before its last die, and
/// the widest range, of 2³² values, which is refused at once.
#[test]
fn test_metered_vast_state_spaces()
{
	let builder = serial_builder("{n}: {n}D6");
	assert!(matches!(
		builder.build_metered([i32::MAX], 10_000),
		Err(EvaluationError::HistogramBudgetExhausted { .. })
	));
	let builder = serial_builder("{a}, {b}: [{a}:{b}]");
	assert_eq!(
		builder.build_metered([i32::MIN, i32::MAX], 10_000),
		Err(EvaluationError::HistogramBudgetExhausted {
			requested: 1 << 32,
			remaining: 10_000,
			consumed: 0
		})
	);
}

/// Test that the builder enumerates the widest range, whose width does not fit
/// in an [`i32`], from its start, without overflowing.
#[test]
fn test_widest_range()
{
	let histogram = serial_builder("{a}, {b}: [{a}:{b}]")
		.build_with_limit([i32::MIN, i32::MAX], 3)
		.unwrap();
	assert_eq!(
		histogram.iter().collect::<BTreeMap<_, _>>(),
		BTreeMap::from([
			(&i32::MIN, &1),
			(&(i32::MIN + 1), &1),
			(&(i32::MIN + 2), &1)
		])
	);
}

/// Test that the parallel builder agrees with the serial builder on exactly
/// which budgets suffice, whatever the number of threads and however often it
/// runs, and that its builds within budget are exactly the serial builds.
#[cfg(feature = "parallel-histogram")]
#[test]
fn test_parallel_metered_agrees()
{
	for (source, args) in [
		("3D6", &[][..]),
		("(1D3)D3", &[]),
		("{n}: {n}D4 drop lowest 1", &[5]),
		("[1:6] * 2D[1,3,5] + 1D10", &[])
	]
	{
		let least = least_budget(source, args);
		let serial =
			serial_builder(source).build(args.iter().copied()).unwrap();
		let builder = parallel_builder(source);
		for threads in [1, 4]
		{
			for _ in 0..8
			{
				let (within, beyond) = in_pool(threads, || {
					(
						builder.build_metered(args.iter().copied(), least),
						builder.build_metered(args.iter().copied(), least - 1)
					)
				});
				assert_eq!(within.as_ref(), Ok(&serial), "{}", source);
				assert!(
					matches!(
						beyond,
						Err(EvaluationError::HistogramBudgetExhausted { .. })
					),
					"{}: {:?}",
					source,
					beyond
				);
			}
		}
	}
}

/// Test that the parallel builder refuses the vast state spaces promptly, and
/// leaves no partial histogram behind to spoil a later build.
#[cfg(feature = "parallel-histogram")]
#[test]
fn test_parallel_metered_vast_state_spaces()
{
	let builder = parallel_builder("{n}: {n}D6");
	for threads in [1, 4]
	{
		let refused =
			in_pool(threads, || builder.build_metered([i32::MAX], 10_000));
		assert!(
			matches!(
				refused,
				Err(EvaluationError::HistogramBudgetExhausted { .. })
			),
			"{:?}",
			refused
		);
		let histogram = in_pool(threads, || builder.build([2]).unwrap());
		assert_eq!(histogram.total(), 36);
	}
	let builder = parallel_builder("{a}, {b}: [{a}:{b}]");
	assert_eq!(
		in_pool(4, || builder.build_metered([i32::MIN, i32::MAX], 10_000)),
		Err(EvaluationError::HistogramBudgetExhausted {
			requested: 1 << 32,
			remaining: 10_000,
			consumed: 0
		})
	);
}