Unnamed repository; edit this file 'description' to name the repository.
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
use crate::implementation::{TestContext, TestContextImpl};
use salsa::debug::DebugQueryTable;
use salsa::Durability;

#[salsa::query_group(Constants)]
pub(crate) trait ConstantsDatabase: TestContext {
    #[salsa::input]
    fn input(&self, key: char) -> usize;

    fn add(&self, key1: char, key2: char) -> usize;

    fn add3(&self, key1: char, key2: char, key3: char) -> usize;
}

fn add(db: &dyn ConstantsDatabase, key1: char, key2: char) -> usize {
    db.log().add(format!("add({key1}, {key2})"));
    db.input(key1) + db.input(key2)
}

fn add3(db: &dyn ConstantsDatabase, key1: char, key2: char, key3: char) -> usize {
    db.log().add(format!("add3({key1}, {key2}, {key3})"));
    db.add(key1, key2) + db.input(key3)
}

// Test we can assign a constant and things will be correctly
// recomputed afterwards.
#[test]
fn invalidate_constant() {
    let db = &mut TestContextImpl::default();
    db.set_input_with_durability('a', 44, Durability::HIGH);
    db.set_input_with_durability('b', 22, Durability::HIGH);
    assert_eq!(db.add('a', 'b'), 66);

    db.set_input_with_durability('a', 66, Durability::HIGH);
    assert_eq!(db.add('a', 'b'), 88);
}

#[test]
fn invalidate_constant_1() {
    let db = &mut TestContextImpl::default();

    // Not constant:
    db.set_input('a', 44);
    assert_eq!(db.add('a', 'a'), 88);

    // Becomes constant:
    db.set_input_with_durability('a', 44, Durability::HIGH);
    assert_eq!(db.add('a', 'a'), 88);

    // Invalidates:
    db.set_input_with_durability('a', 33, Durability::HIGH);
    assert_eq!(db.add('a', 'a'), 66);
}

// Test cases where we assign same value to 'a' after declaring it a
// constant.
#[test]
fn set_after_constant_same_value() {
    let db = &mut TestContextImpl::default();
    db.set_input_with_durability('a', 44, Durability::HIGH);
    db.set_input_with_durability('a', 44, Durability::HIGH);
    db.set_input('a', 44);
}

#[test]
fn not_constant() {
    let mut db = TestContextImpl::default();

    db.set_input('a', 22);
    db.set_input('b', 44);
    assert_eq!(db.add('a', 'b'), 66);
    assert_eq!(Durability::LOW, AddQuery.in_db(&db).durability(('a', 'b')));
}

#[test]
fn durability() {
    let mut db = TestContextImpl::default();

    db.set_input_with_durability('a', 22, Durability::HIGH);
    db.set_input_with_durability('b', 44, Durability::HIGH);
    assert_eq!(db.add('a', 'b'), 66);
    assert_eq!(Durability::HIGH, AddQuery.in_db(&db).durability(('a', 'b')));
}

#[test]
fn mixed_constant() {
    let mut db = TestContextImpl::default();

    db.set_input_with_durability('a', 22, Durability::HIGH);
    db.set_input('b', 44);
    assert_eq!(db.add('a', 'b'), 66);
    assert_eq!(Durability::LOW, AddQuery.in_db(&db).durability(('a', 'b')));
}

#[test]
fn becomes_constant_with_change() {
    let mut db = TestContextImpl::default();

    db.set_input('a', 22);
    db.set_input('b', 44);
    assert_eq!(db.add('a', 'b'), 66);
    assert_eq!(Durability::LOW, AddQuery.in_db(&db).durability(('a', 'b')));

    db.set_input_with_durability('a', 23, Durability::HIGH);
    assert_eq!(db.add('a', 'b'), 67);
    assert_eq!(Durability::LOW, AddQuery.in_db(&db).durability(('a', 'b')));

    db.set_input_with_durability('b', 45, Durability::HIGH);
    assert_eq!(db.add('a', 'b'), 68);
    assert_eq!(Durability::HIGH, AddQuery.in_db(&db).durability(('a', 'b')));

    db.set_input_with_durability('b', 45, Durability::MEDIUM);
    assert_eq!(db.add('a', 'b'), 68);
    assert_eq!(Durability::MEDIUM, AddQuery.in_db(&db).durability(('a', 'b')));
}

// Test a subtle case in which an input changes from constant to
// non-constant, but its value doesn't change. If we're not careful,
// this can cause us to incorrectly consider derived values as still
// being constant.
#[test]
fn constant_to_non_constant() {
    let mut db = TestContextImpl::default();

    db.set_input_with_durability('a', 11, Durability::HIGH);
    db.set_input_with_durability('b', 22, Durability::HIGH);
    db.set_input_with_durability('c', 33, Durability::HIGH);

    // Here, `add3` invokes `add`, which yields 33. Both calls are
    // constant.
    assert_eq!(db.add3('a', 'b', 'c'), 66);

    db.set_input('a', 11);

    // Here, `add3` invokes `add`, which *still* yields 33, but which
    // is no longer constant. Since value didn't change, we might
    // preserve `add3` unchanged, not noticing that it is no longer
    // constant.
    assert_eq!(db.add3('a', 'b', 'c'), 66);

    // In that case, we would not get the correct result here, when
    // 'a' changes *again*.
    db.set_input('a', 22);
    assert_eq!(db.add3('a', 'b', 'c'), 77);
}