/
index.test.ts
356 lines (298 loc) · 13.5 KB
/
index.test.ts
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
import { AND, NAND, NOR, OR, XOR, XNOR, NOT } from './index'
describe('AND', () => {
// Test that the AND function returns true when given two true values
it('should return true when given two true values', () => {
const result = AND(true, true)
// Assert
expect(result).toEqual(true)
})
// Test that the AND function returns true when given three true values
it('should return true when given three true values', () => {
// Arrange
const expression = [true, true, true]
// Act
const result = AND(...expression)
// Assert
expect(result).toBe(true)
})
// Test that the AND function returns true when there is only one true value in the expression.
it('should return true when there is only one true value in the expression', () => {
expect(AND(true)).toBe(true)
})
// Test that the AND function returns false when there is at least one false value in the expression
it('should return false when there is at least one false value in the expression', () => {
expect(AND(true, true, false)).toBe(false)
expect(AND(true, false, true)).toBe(false)
expect(AND(false, true, true)).toBe(false)
expect(AND(false, false, false)).toBe(false)
})
// Test that the AND function returns true when all values in the expression are true, and false otherwise.
it('should return true when all values are true', () => {
const result = AND(true, true, true, true, true)
expect(result).toBe(true)
})
// Test that the AND function returns false when there are multiple false values in the expression
it('should return false when there are multiple false values', () => {
expect(AND(false, false, false)).toBe(false)
expect(AND(false, true, false)).toBe(false)
expect(AND(true, false, false)).toBe(false)
expect(AND(false, false, true)).toBe(false)
expect(AND(false, true, true)).toBe(false)
expect(AND(true, false, true)).toBe(false)
expect(AND(true, true, false)).toBe(false)
})
// Test that the AND function returns true when all input values are true, regardless of their order.
it('should return true when all input values are true', () => {
expect(AND(true, true, true)).toBe(true)
expect(AND(true, true, false)).toBe(false)
expect(AND(false, true, true)).toBe(false)
expect(AND(true, false, true)).toBe(false)
expect(AND(false, false, false)).toBe(false)
})
// Test that the 'AND' function returns true only if all the input values are true, and false otherwise.
it('should return true when all input values are true', () => {
expect(AND(true, true, true)).toBe(true)
expect(AND(true, true, false)).toBe(false)
expect(AND(true, false, true)).toBe(false)
expect(AND(false, true, true)).toBe(false)
expect(AND(false, false, false)).toBe(false)
})
it('should return true with 1000 true values', () => {
const expression = Array.from({ length: 1000 }, () => true)
expect(AND(...expression)).toBe(true)
})
it('should return false with 1000 false values', () => {
const expression = Array.from({ length: 1000 }, () => false)
expect(AND(...expression)).toBe(false)
})
it('should return false with 50000 true values and one false value', () => {
const expression = Array.from({ length: 50000 }, () => true)
expression.push(false)
expect(AND(...expression)).toBe(false)
})
})
describe('NAND', () => {
// Test that the NAND function returns true when all inputs are false
it('should return true when all inputs are false', () => {
expect(NAND(false, false)).toBe(true)
expect(NAND(false, false, false)).toBe(true)
expect(NAND(false, false, false, false)).toBe(true)
})
// Test that the NAND function returns false when at least one input is true
it('should return false when at least one input is true', () => {
expect(NAND(true, true)).toBe(false)
})
// Test that the NAND function returns true when given one input that is true
it('should return true when given one input that is true', () => {
expect(NAND(false)).toBe(true)
expect(NAND(true, false)).toBe(true)
expect(NAND(false, true)).toBe(true)
expect(NAND(true, true)).toBe(false)
})
// Test that the NAND function returns true when given one input that is false
it('should return true when given one false input', () => {
expect(NAND(false)).toBe(true)
expect(NAND(true)).toBe(false)
expect(NAND(false, true)).toBe(true)
expect(NAND(true, false)).toBe(true)
expect(NAND(false, false)).toBe(true)
expect(NAND(true, true)).toBe(false)
})
// Test that the NAND function returns false when given only true inputs
it('should return false when given only true inputs', () => {
expect(NAND(true)).toBe(false)
expect(NAND(true, true)).toBe(false)
expect(NAND(true, true, true)).toBe(false)
})
// Test that the NAND function works correctly with any number of inputs
it('should return the correct result when given any number of inputs', () => {
// Test case 2: 1 input
expect(NAND(true)).toBe(false)
expect(NAND(false)).toBe(true)
// Test case 3: 2 inputs
expect(NAND(true, true)).toBe(false)
expect(NAND(true, false)).toBe(true)
expect(NAND(false, true)).toBe(true)
expect(NAND(false, false)).toBe(true)
// Test case 4: 3 inputs
expect(NAND(true, true, true)).toBe(false)
expect(NAND(true, true, false)).toBe(true)
expect(NAND(true, false, true)).toBe(true)
expect(NAND(true, false, false)).toBe(true)
expect(NAND(false, true, true)).toBe(true)
expect(NAND(false, true, false)).toBe(true)
expect(NAND(false, false, true)).toBe(true)
expect(NAND(false, false, false)).toBe(true)
// Add more test cases for higher number of inputs if needed
})
// Test that the NAND function returns true when given only false inputs
it('should return true when given only false inputs', () => {
expect(NAND(false, false, false)).toBe(true)
expect(NAND(false, false)).toBe(true)
expect(NAND(false)).toBe(true)
})
// Test that the NAND function returns true when given undefined inputs
it('should return true when given undefined inputs', () => {
expect(NAND(undefined)).toBe(true)
expect(NAND(undefined, undefined)).toBe(true)
expect(NAND(undefined, undefined, undefined)).toBe(true)
})
})
describe('OR', () => {
// Test that the OR function returns true when given two true values as arguments.
it('should return true when given two true values', () => {
expect(OR(true, true)).toBe(true)
})
// Test that the OR function returns true when there are multiple true values in the input array
it('should return true when there are multiple true values', () => {
expect(OR(true, true, true)).toBe(true)
})
// Test that the OR function returns true if at least one of the boolean values is true, and false otherwise.
it('should return true when at least one boolean value is true', () => {
expect(OR(true, false, false)).toBe(true)
expect(OR(false, true, false)).toBe(true)
expect(OR(false, false, true)).toBe(true)
expect(OR(true, true, false)).toBe(true)
expect(OR(true, false, true)).toBe(true)
expect(OR(false, true, true)).toBe(true)
expect(OR(true, true, true)).toBe(true)
expect(OR(false, false, false)).toBe(false)
})
})
describe('NOR', () => {
// Test that NOR function returns true when both input expressions are false.
it('should return true when both input expressions are false', () => {
expect(NOR(false, false)).toBe(true)
})
// Test that NOR function returns false when given false and true as arguments.
it('should return false when given false and true', () => {
expect(NOR(false, true)).toBe(false)
})
// Test that the NOR function returns false when given the arguments true and false.
it('should return false when given true and false', () => {
expect(NOR(true, false)).toBe(false)
})
// Test that NOR function returns false when both input expressions are true.
it('should return false when both input expressions are true', () => {
expect(NOR(true, true)).toBe(false)
})
// Test that NOR() returns true
it('should return true when NOR() is called', () => {
expect(NOR()).toBe(true)
})
// Test that NOR(false) returns true.
it('should return true when input is false', () => {
expect(NOR(false)).toBe(true)
})
// Test that the NOR function returns false when the input is true.
it('should return false when input is true', () => {
expect(NOR(true)).toBe(false)
})
})
describe('XOR', () => {
// Test that the XOR function returns false when given two false values as arguments.
it('should return false when given two false values', () => {
expect(XOR(false, false)).toBe(false)
})
// Test that the XOR function returns true when there is exactly one true value in the expression array and false otherwise.
it('should return true when there is exactly one true value in the expression array', () => {
expect(XOR(true, false)).toBe(true)
})
// Test that the XOR function returns false when given three false values
it('should return false when given three false values', () => {
expect(XOR(false, false, false)).toBe(false)
})
// Test that the XOR function returns false when given an empty input
it('should return false when given an empty input', () => {
expect(XOR()).toBe(false)
})
// Test that the XOR function returns the correct result when given one input value.
it('should return true when the input value is true', () => {
expect(XOR(true)).toBe(true)
})
// Test that the XOR function returns false when all input values are false.
it('should return false when all input values are false', () => {
expect(XOR(false, false, false)).toBe(false)
})
// Test that the XOR function returns false when all input values are true.
it('should return false when all input values are true', () => {
expect(XOR(true, true, true)).toBe(false)
})
// Test that the XOR function correctly handles a large number of input values
it('should return true when there is exactly one true value in the input', () => {
// Generate a large number of input values
const input = Array.from({ length: 10000 }, () => Math.random() < 0.5)
// Call the XOR function with the input values
const result = XOR(...input)
// Count the number of true values in the input
const trueCount = input.filter((value) => value === true).length
// Assert that the result is true if and only if there is exactly one true value in the input
expect(result).toBe(trueCount === 1)
})
// Test that the XOR function returns true when there is exactly one true value in the input array, and false otherwise.
it('should return true when there is exactly one true value in the input array', () => {
expect(XOR(true)).toBe(true)
expect(XOR(false)).toBe(false)
expect(XOR(true, false)).toBe(true)
expect(XOR(false, false)).toBe(false)
expect(XOR(true, true)).toBe(false)
expect(XOR(true, false, true)).toBe(false)
expect(XOR(true, false, false)).toBe(true)
expect(XOR(true, true, true)).toBe(false)
})
// Test that the XOR function returns the expected result when input values are null or undefined
it('should return false when input values are null or undefined', () => {
expect(XOR(null)).toBe(false)
expect(XOR(undefined)).toBe(false)
expect(XOR(null, null)).toBe(false)
expect(XOR(undefined, undefined)).toBe(false)
expect(XOR(null, undefined)).toBe(false)
expect(XOR(undefined, null)).toBe(false)
})
})
describe('XNOR', () => {
// Test that XNOR function returns true when both input parameters are true.
it('should return true when both input parameters are true', () => {
expect(XNOR(true, true)).toBe(true)
})
// Test that XNOR function returns true when both input values are false.
it('should return true when both input values are false', () => {
expect(XNOR(false, false)).toBe(true)
})
// Test that XNOR function returns false when given true and false as arguments.
it('should return false when given true and false', () => {
expect(XNOR(true, false)).toBe(false)
})
// Test that XNOR function returns false when given false and true as arguments.
it('should return false when given false and true as arguments', () => {
expect(XNOR(false, true)).toBe(false)
})
// Test that XNOR function returns true when both arguments are null.
it('should return true when both arguments are null', () => {
expect(XNOR(null, null)).toBe(true)
})
// Test that XNOR function returns true when both arguments are undefined.
it('should return true when both arguments are undefined', () => {
expect(XNOR(undefined, undefined)).toBe(true)
})
// Test that XNOR function returns false when the input is true and null.
it('should return false when input is true and null', () => {
expect(XNOR(true, null)).toBe(false)
})
})
describe('NOT', () => {
// Test that the NOT function returns an array of boolean values where each value is the negation of the corresponding value in the input expression array when a single true value is passed as input.
it('should return an array of negated boolean values when a single true value is passed as input', () => {
const result = NOT(true)
expect(result).toEqual([false])
})
// Test that the NOT function correctly negates each boolean value in the input expression array
it('should correctly negate each boolean value in the input expression array', () => {
// Arrange
const expression = [true, false, true, false]
// Act
const result = NOT(...expression)
// Assert
expect(result).toEqual([false, true, false, true])
})
})