1
1
use naga:: { valid, Expression , Function , Scalar } ;
2
2
3
+ /// Validation should fail if `AtomicResult` expressions are not
4
+ /// populated by `Atomic` statements.
3
5
#[ test]
4
- fn emit_atomic_result ( ) {
6
+ fn populate_atomic_result ( ) {
5
7
use naga:: { Module , Type , TypeInner } ;
6
8
7
- // We want to ensure that the *only* problem with the code is the
8
- // use of an `Emit` statement instead of an `Atomic` statement. So
9
- // validate two versions of the module varying only in that
10
- // aspect.
11
- //
12
- // Looking at uses of the `atomic` makes it easy to identify the
13
- // differences between the two variants.
14
- fn variant (
15
- atomic : bool ,
9
+ /// Different variants of the test case that we want to exercise.
10
+ enum Variant {
11
+ /// An `AtomicResult` expression with an `Atomic` statement
12
+ /// that populates it: valid.
13
+ Atomic ,
14
+
15
+ /// An `AtomicResult` expression visited by an `Emit`
16
+ /// statement: invalid.
17
+ Emit ,
18
+
19
+ /// An `AtomicResult` expression visited by no statement at
20
+ /// all: invalid
21
+ None ,
22
+ }
23
+
24
+ // Looking at uses of `variant` should make it easy to identify
25
+ // the differences between the test cases.
26
+ fn try_variant (
27
+ variant : Variant ,
16
28
) -> Result < naga:: valid:: ModuleInfo , naga:: WithSpan < naga:: valid:: ValidationError > > {
17
29
let span = naga:: Span :: default ( ) ;
18
30
let mut module = Module :: default ( ) ;
@@ -56,21 +68,25 @@ fn emit_atomic_result() {
56
68
span,
57
69
) ;
58
70
59
- if atomic {
60
- fun. body . push (
61
- naga:: Statement :: Atomic {
62
- pointer : ex_global,
63
- fun : naga:: AtomicFunction :: Add ,
64
- value : ex_42,
65
- result : ex_result,
66
- } ,
67
- span,
68
- ) ;
69
- } else {
70
- fun. body . push (
71
- naga:: Statement :: Emit ( naga:: Range :: new_from_bounds ( ex_result, ex_result) ) ,
72
- span,
73
- ) ;
71
+ match variant {
72
+ Variant :: Atomic => {
73
+ fun. body . push (
74
+ naga:: Statement :: Atomic {
75
+ pointer : ex_global,
76
+ fun : naga:: AtomicFunction :: Add ,
77
+ value : ex_42,
78
+ result : ex_result,
79
+ } ,
80
+ span,
81
+ ) ;
82
+ }
83
+ Variant :: Emit => {
84
+ fun. body . push (
85
+ naga:: Statement :: Emit ( naga:: Range :: new_from_bounds ( ex_result, ex_result) ) ,
86
+ span,
87
+ ) ;
88
+ }
89
+ Variant :: None => { }
74
90
}
75
91
76
92
module. functions . append ( fun, span) ;
@@ -82,23 +98,34 @@ fn emit_atomic_result() {
82
98
. validate ( & module)
83
99
}
84
100
85
- variant ( true ) . expect ( "module should validate" ) ;
86
- assert ! ( variant( false ) . is_err( ) ) ;
101
+ try_variant ( Variant :: Atomic ) . expect ( "module should validate" ) ;
102
+ assert ! ( try_variant( Variant :: Emit ) . is_err( ) ) ;
103
+ assert ! ( try_variant( Variant :: None ) . is_err( ) ) ;
87
104
}
88
105
89
106
#[ test]
90
- fn emit_call_result ( ) {
107
+ fn populate_call_result ( ) {
91
108
use naga:: { Module , Type , TypeInner } ;
92
109
93
- // We want to ensure that the *only* problem with the code is the
94
- // use of an `Emit` statement instead of a `Call` statement. So
95
- // validate two versions of the module varying only in that
96
- // aspect.
97
- //
98
- // Looking at uses of the `call` makes it easy to identify the
99
- // differences between the two variants.
100
- fn variant (
101
- call : bool ,
110
+ /// Different variants of the test case that we want to exercise.
111
+ enum Variant {
112
+ /// A `CallResult` expression with an `Call` statement that
113
+ /// populates it: valid.
114
+ Call ,
115
+
116
+ /// A `CallResult` expression visited by an `Emit` statement:
117
+ /// invalid.
118
+ Emit ,
119
+
120
+ /// A `CallResult` expression visited by no statement at all:
121
+ /// invalid
122
+ None ,
123
+ }
124
+
125
+ // Looking at uses of `variant` should make it easy to identify
126
+ // the differences between the test cases.
127
+ fn try_variant (
128
+ variant : Variant ,
102
129
) -> Result < naga:: valid:: ModuleInfo , naga:: WithSpan < naga:: valid:: ValidationError > > {
103
130
let span = naga:: Span :: default ( ) ;
104
131
let mut module = Module :: default ( ) ;
@@ -130,20 +157,24 @@ fn emit_call_result() {
130
157
. expressions
131
158
. append ( Expression :: CallResult ( fun_callee) , span) ;
132
159
133
- if call {
134
- fun_caller. body . push (
135
- naga:: Statement :: Call {
136
- function : fun_callee,
137
- arguments : vec ! [ ] ,
138
- result : Some ( ex_result) ,
139
- } ,
140
- span,
141
- ) ;
142
- } else {
143
- fun_caller. body . push (
144
- naga:: Statement :: Emit ( naga:: Range :: new_from_bounds ( ex_result, ex_result) ) ,
145
- span,
146
- ) ;
160
+ match variant {
161
+ Variant :: Call => {
162
+ fun_caller. body . push (
163
+ naga:: Statement :: Call {
164
+ function : fun_callee,
165
+ arguments : vec ! [ ] ,
166
+ result : Some ( ex_result) ,
167
+ } ,
168
+ span,
169
+ ) ;
170
+ }
171
+ Variant :: Emit => {
172
+ fun_caller. body . push (
173
+ naga:: Statement :: Emit ( naga:: Range :: new_from_bounds ( ex_result, ex_result) ) ,
174
+ span,
175
+ ) ;
176
+ }
177
+ Variant :: None => { }
147
178
}
148
179
149
180
module. functions . append ( fun_caller, span) ;
@@ -155,8 +186,9 @@ fn emit_call_result() {
155
186
. validate ( & module)
156
187
}
157
188
158
- variant ( true ) . expect ( "should validate" ) ;
159
- assert ! ( variant( false ) . is_err( ) ) ;
189
+ try_variant ( Variant :: Call ) . expect ( "should validate" ) ;
190
+ assert ! ( try_variant( Variant :: Emit ) . is_err( ) ) ;
191
+ assert ! ( try_variant( Variant :: None ) . is_err( ) ) ;
160
192
}
161
193
162
194
#[ test]
0 commit comments