-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.cpp
More file actions
788 lines (687 loc) · 26 KB
/
test.cpp
File metadata and controls
788 lines (687 loc) · 26 KB
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
// Standalone test suite for expr3.h
// Build: g++ -std=c++17 -Wall -o test test.cpp && ./test
// cl /std:c++17 /W4 /EHsc test.cpp /Fetest.exe
#include "expr3.h"
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
using namespace expr3;
// ---------------------------------------------------------------------------
// Harness
// ---------------------------------------------------------------------------
static int g_pass = 0, g_fail = 0;
// Boolean condition (ok flags, is_error(), etc.)
static void check(bool ok, const char* name)
{
if (ok) { ++g_pass; return; }
++g_fail;
std::cout << " [FAIL] " << name << "\n"
<< " expected: true\n"
<< " actual: false\n";
}
// Generic value equality — prints expected/actual on failure
template<typename T>
static void check_eq(T actual, T expected, const char* name)
{
if (actual == expected) { ++g_pass; return; }
++g_fail;
std::cout << " [FAIL] " << name << "\n"
<< " expected: " << expected << "\n"
<< " actual: " << actual << "\n";
}
// uint64_t — show in hex
template<>
void check_eq<uint64_t>(uint64_t actual, uint64_t expected, const char* name)
{
if (actual == expected) { ++g_pass; return; }
++g_fail;
std::cout << " [FAIL] " << name << "\n"
<< std::hex << std::showbase
<< " expected: " << expected << "\n"
<< " actual: " << actual << "\n"
<< std::dec << std::noshowbase;
}
// Floating-point near-equality
static bool near(double a, double b) { return std::abs(a - b) < 1e-9; }
static void check_near(double actual, double expected, const char* name)
{
if (near(actual, expected)) { ++g_pass; return; }
++g_fail;
std::cout << " [FAIL] " << name << "\n"
<< std::setprecision(15)
<< " expected: ~" << expected << "\n"
<< " actual: " << actual << "\n";
}
// ---------------------------------------------------------------------------
// Variable/assignment context
// ---------------------------------------------------------------------------
class MapContext : public expr_eval_context
{
public:
std::map<std::string, uint64_t> vars;
Token resolve_var_if_needed(const Token& t) override
{
if (t.type == Token::Type::Number && !t.is_integer() && !t.is_double()) {
auto it = vars.find(t.str);
if (it != vars.end())
return Token::make_constant(it->second);
}
return t;
}
bool assign(const Token& dest, const Token& val) override
{
if (dest.type == Token::Type::Number && !dest.is_integer() && !dest.is_double())
vars[dest.str] = val.as_integer();
return true;
}
Token exec_function(const Token&, std::vector<Token>&) override { return {}; }
};
// Float variable context
class FloatMapCtx : public expr_eval_context {
public:
std::map<std::string, double> vars;
Token resolve_var_if_needed(const Token& t) override {
if (t.type == Token::Type::Number && !t.is_integer() && !t.is_double()) {
auto it = vars.find(t.str);
if (it != vars.end())
return Token(it->second);
}
return t;
}
bool assign(const Token& dest, const Token& val) override {
if (dest.type == Token::Type::Number && !dest.is_integer() && !dest.is_double())
vars[dest.str] = val.as_double();
return true;
}
Token exec_function(const Token&, std::vector<Token>&) override { return {}; }
};
// Variable context that also forwards to built-in functions (max, min)
class MapWithFuncsContext : public expr_eval_context {
public:
std::map<std::string, uint64_t> vars;
Token resolve_var_if_needed(const Token& t) override {
if (t.type == Token::Type::Number && !t.is_integer() && !t.is_double()) {
auto it = vars.find(t.str);
if (it != vars.end())
return Token::make_constant(it->second);
}
return t;
}
bool assign(const Token& dest, const Token& val) override {
if (dest.type == Token::Type::Number && !dest.is_integer() && !dest.is_double())
vars[dest.str] = val.as_integer();
return true;
}
Token exec_function(const Token& func, std::vector<Token>& args) override {
default_expr_eval_context dc;
return dc.exec_function(func, args);
}
};
// Convenience eval helpers
static uint64_t eu(const char* s, bool* ok = nullptr) { expr3u e(s); return e.eval(ok); }
static int64_t es(const char* s, bool* ok = nullptr) { expr3s e(s); return e.eval(ok); }
static double ef(const char* s, bool* ok = nullptr) { expr3f e(s); return e.eval(ok); }
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
static void test_arithmetic()
{
std::cout << "\n=== Basic Arithmetic ===\n";
check_eq(eu("1+2"), 3ULL, "1+2");
check_eq(eu("10-3"), 13ULL, "10-3"); // 0x10-3 = 16-3 = 13
check_eq(eu("3*4"), 12ULL, "3*4");
check_eq(eu("8/2"), 4ULL, "8/2");
check_eq(eu("7%3"), 1ULL, "7%3");
check_eq(eu("2+3*4"), 14ULL, "2+3*4 (precedence)");
check_eq(eu("(2+3)*4"), 20ULL, "(2+3)*4");
check_eq(eu("10-3-2"), 11ULL, "10-3-2 (left-assoc)"); // 0x10-3-2 = 11
check_eq(eu("2*3+4*5"), 26ULL, "2*3+4*5");
}
static void test_bitwise()
{
std::cout << "\n=== Bitwise Operators ===\n";
check_eq(eu("0xF0&0x0F"), 0x00ULL, "0xF0 & 0x0F");
check_eq(eu("0xF0|0x0F"), 0xFFULL, "0xF0 | 0x0F");
check_eq(eu("0xFF^0x0F"), 0xF0ULL, "0xFF ^ 0x0F");
check_eq(eu("~0"), 0xFFFFFFFFFFFFFFFFULL, "~0");
check_eq(eu("~0xFF"), 0xFFFFFFFFFFFFFF00ULL, "~0xFF");
check_eq(eu("1|2&3"), 3ULL, "1|2&3 (& before |)");
check_eq(eu("~0&0xFF"), 0xFFULL, "~0&0xFF (~ before &)");
}
static void test_shifts_and_rotates()
{
std::cout << "\n=== Shifts and Rotates ===\n";
check_eq(eu("1<<4"), 16ULL, "1<<4");
check_eq(eu("16>>2"), 5ULL, "16>>2"); // 0x16>>2 = 22>>2 = 5
check_eq(eu("0xFF<<8>>4"), 0xFF0ULL, "0xFF<<8>>4");
check_eq(eu("0x8000000000000000<<<1"), 1ULL, "0x8000000000000000<<<1 (ROL)");
check_eq(eu("1>>>1"), 0x8000000000000000ULL, "1>>>1 (ROR)");
}
static void test_unary_integer()
{
std::cout << "\n=== Unary Operators (integer) ===\n";
check_eq(eu("!0"), 1ULL, "!0");
check_eq(eu("!1"), 0ULL, "!1");
check_eq(eu("!5"), 0ULL, "!5");
check_eq(eu("!!1"), 1ULL, "!!1");
check_eq(es("-5"), -5LL, "-5");
check_eq(es("-(3)"), -3LL, "-(3)");
check_eq(es("-(-3)"), 3LL, "-(-3)");
}
// BUG #2 (fixed): float ~ was using li instead of ri
// BUG #3 (fixed): float ! was using li instead of ri
static void test_unary_float()
{
std::cout << "\n=== Float Unary Ops ===\n";
check_near(ef("!1.0"), 0.0, "float !1.0");
check_near(ef("!5.0"), 0.0, "float !5.0");
check_near(ef("!0.0"), 1.0, "float !0.0");
}
// BUG #1 (fixed): && and || were never produced by the tokenizer
static void test_logical()
{
std::cout << "\n=== Logical && / || ===\n";
check_eq(eu("1&&1"), 1ULL, "1&&1");
check_eq(eu("2&&1"), 1ULL, "2&&1");
check_eq(eu("1&&0"), 0ULL, "1&&0");
check_eq(eu("0&&1"), 0ULL, "0&&1");
check_eq(eu("0&&0"), 0ULL, "0&&0");
check_eq(eu("1&&1&&1"),1ULL, "1&&1&&1");
check_eq(eu("0||0"), 0ULL, "0||0");
check_eq(eu("0||1"), 1ULL, "0||1");
check_eq(eu("1||0"), 1ULL, "1||0");
check_eq(eu("2||3"), 1ULL, "2||3");
}
static void test_comparisons()
{
std::cout << "\n=== Comparison Operators ===\n";
check_eq(eu("3==3"), 1ULL, "3==3");
check_eq(eu("3==4"), 0ULL, "3==4");
check_eq(eu("3!=4"), 1ULL, "3!=4");
check_eq(eu("3!=3"), 0ULL, "3!=3");
check_eq(eu("3<4"), 1ULL, "3<4");
check_eq(eu("4<3"), 0ULL, "4<3");
check_eq(eu("3<=3"), 1ULL, "3<=3");
check_eq(eu("3<=4"), 1ULL, "3<=4");
check_eq(eu("4<=3"), 0ULL, "4<=3");
check_eq(eu("4>3"), 1ULL, "4>3");
check_eq(eu("3>4"), 0ULL, "3>4");
check_eq(eu("4>=4"), 1ULL, "4>=4");
check_eq(eu("4>=3"), 1ULL, "4>=3");
check_eq(eu("3>=4"), 0ULL, "3>=4");
check_eq(eu("1+2==3"), 1ULL, "1+2==3");
check_eq(eu("1+1==3"), 0ULL, "1+1==3");
}
static void test_float_arithmetic()
{
std::cout << "\n=== Float Arithmetic ===\n";
check_near(ef("1.5+2.5"), 4.0, "1.5+2.5");
check_near(ef("3.0*2.5"), 7.5, "3.0*2.5");
check_near(ef("7.5/2.5"), 3.0, "7.5/2.5");
check_near(ef("2.5-1.0"), 1.5, "2.5-1.0");
check_near(ef("7.5%2.5"), 0.0, "7.5%2.5 (fmod)");
check_near(ef("5.0%3.0"), 2.0, "5.0%3.0 (fmod)");
check_near(ef("(1.5+2.5)*2.0"), 8.0, "(1.5+2.5)*2.0");
}
static void test_functions()
{
std::cout << "\n=== Built-in Functions ===\n";
check_eq(eu("max(3,5)"), 5ULL, "max(3,5)");
check_eq(eu("max(5,3)"), 5ULL, "max(5,3)");
check_eq(eu("min(3,5)"), 3ULL, "min(3,5)");
check_eq(eu("min(5,3)"), 3ULL, "min(5,3)");
check_near(ef("max(1.5,2.5)"), 2.5, "max(1.5,2.5)");
check_near(ef("min(1.5,2.5)"), 1.5, "min(1.5,2.5)");
check_eq(eu("max(max(1,3),2)"), 3ULL, "max(max(1,3),2)");
}
static void test_variables()
{
std::cout << "\n=== Variables via Context ===\n";
MapContext ctx;
ctx.vars["x"] = 42;
ctx.vars["z"] = 8;
expr3u e;
bool ok;
e.set_from_string("x+z");
check_eq(e.eval(&ok, &ctx), 50ULL, "x+z (x=42, z=8)");
check(ok, "x+z ok");
e.set_from_string("x*z");
check_eq(e.eval(&ok, &ctx), 336ULL, "x*z");
e.set_from_string("x-z");
check_eq(e.eval(&ok, &ctx), 34ULL, "x-z");
expr3f ef2("pi");
double pi_val = ef2.eval(&ok);
check(ok, "pi ok");
check_near(pi_val, 3.1415926535, "pi value");
}
static void test_assignment_ops()
{
std::cout << "\n=== Assignment Operators ===\n";
MapContext ctx;
expr3u e;
bool ok;
#define ASSIGN_TEST(init, expr_str, expected, label) \
ctx.vars["x"] = (init); \
e.set_from_string(expr_str); \
e.eval(&ok, &ctx); \
check(ok, label " ok"); \
check_eq(ctx.vars["x"], (uint64_t)(expected), label)
ASSIGN_TEST(10, "x+=5", 15, "x+=5");
ASSIGN_TEST(10, "x-=3", 7, "x-=3");
ASSIGN_TEST(10, "x*=2", 20, "x*=2");
ASSIGN_TEST(10, "x/=2", 5, "x/=2");
ASSIGN_TEST(0xF0, "x&=0x0F", 0, "x&=0x0F");
ASSIGN_TEST(0xF0, "x|=0x0F", 0xFF, "x|=0x0F");
ASSIGN_TEST(0xFF, "x^=0x0F", 0xF0, "x^=0x0F");
ASSIGN_TEST(1, "x<<=3", 8, "x<<=3");
ASSIGN_TEST(0x10, "x>>=2", 4, "x>>=2");
#undef ASSIGN_TEST
}
static void test_signed_arithmetic()
{
std::cout << "\n=== Signed Integer (expr3s) ===\n";
check_eq(es("-5+3"), -2LL, "-5+3");
check_eq(es("10-20"), -16LL, "10-20"); // 0x10-0x20 = 16-32 = -16
check_eq(es("-3*4"), -12LL, "-3*4");
check_eq(es("-10/-2"), 8LL, "-10/-2"); // -0x10/-0x2 = -16/-2 = 8
check_eq(es("1<<3"), 8LL, "1<<3");
check_eq(es("--5"), 5LL, "--5");
}
// BUG #4 (fixed): str_as_double always set ok=true
static void test_str_as_double()
{
std::cout << "\n=== str_as_double correctness ===\n";
Token t_bad(Token::Type::Number, "abc.def");
check_eq(t_bad.is_double(), false, "is_double(\"abc.def\")");
Token t_no_dot(Token::Type::Number, "xyz");
check_eq(t_no_dot.is_double(), false, "is_double(\"xyz\")");
Token t_ok(Token::Type::Number, "3.14");
double v = 0.0;
check_eq(t_ok.is_double(&v), true, "is_double(\"3.14\")");
check_near(v, 3.14, "is_double(\"3.14\") value");
}
// BUG #5: no div-by-zero guard — float is safely testable
static void test_division_by_zero()
{
std::cout << "\n=== Division by Zero ===\n";
check(std::isinf(ef("1.0/0.0")), "1.0/0.0 == inf");
check(std::isnan(ef("5.0%0.0")), "5.0%0.0 == nan (fmod)");
bool ok;
eu("1/0", &ok);
check(!ok, "integer 1/0 returns error");
eu("5%0", &ok);
check(!ok, "integer 5%0 returns error");
}
// BUG #8 (fixed): set_from_string returned Token(data.empty())
static void test_set_from_string_return()
{
std::cout << "\n=== set_from_string return value ===\n";
expr3u e;
Token r_ok = e.set_from_string("1+2");
check(!r_ok.is_error(), "set_from_string(\"1+2\") not error");
check(!r_ok.is_number(), "set_from_string success not a Number token");
Token r_err = e.set_from_string("((1+2)");
check(r_err.is_error(), "set_from_string(\"((1+2)\") is error");
Token r_empty = e.set_from_string("");
check(r_empty.is_error(), "set_from_string(\"\") is error");
}
// BUG #9: is_integer tries base-16 twice; also documents default_base=16
static void test_is_integer_base()
{
std::cout << "\n=== is_integer base fallback ===\n";
Token t1(Token::Type::Number, "10");
uint64_t v1 = 0;
check(t1.is_integer(&v1), "is_integer(\"10\") true");
check_eq(v1, 16ULL, "is_integer(\"10\") == 16 (default_base=16)");
Token t2(Token::Type::Number, "FF");
uint64_t v2 = 0;
check(t2.is_integer(&v2), "is_integer(\"FF\") true");
check_eq(v2, 255ULL, "is_integer(\"FF\") == 255");
Token t3(Token::Type::Number, "99");
uint64_t v3 = 0;
t3.is_integer(&v3);
check_eq(v3, 0x99ULL, "is_integer(\"99\") == 0x99 (hex wins)");
Token t4(Token::Type::Number, "GG");
check_eq(t4.is_integer(), false, "is_integer(\"GG\") false");
}
static void test_error_handling()
{
std::cout << "\n=== Error Handling ===\n";
expr3u e;
bool ok;
check(e.set_from_string("(1+2").is_error(), "\"(1+2\" parse error");
check(e.set_from_string("1+2)").is_error(), "\"1+2)\" parse error");
e.set_from_string("1+");
e.eval(&ok);
check(!ok, "\"1+\" eval fails");
}
static void test_intermediate_repr()
{
std::cout << "\n=== Intermediate (RPN) Representation ===\n";
expr3u e;
e.set_from_string("1+2*3");
check_eq(e.intermediate_repr(), std::string("1 2 3 * + "), "\"1+2*3\" RPN");
e.set_from_string("(1+2)*3");
check_eq(e.intermediate_repr(), std::string("1 2 + 3 * "), "\"(1+2)*3\" RPN");
}
static void test_unary_chain()
{
std::cout << "\n=== Unary Operator Chains ===\n";
check_eq(es("---5"), -5LL, "---5 == -5");
check_eq(es("----5"), 5LL, "----5 == 5");
check_eq(eu("!!!1"), 0ULL, "!!!1 == 0");
check_eq(eu("!!!0"), 1ULL, "!!!0 == 1");
}
static void test_nested_functions()
{
std::cout << "\n=== Nested Functions ===\n";
check_eq(eu("min(max(1,3),2)"), 2ULL, "min(max(1,3),2)");
check_eq(eu("max(min(1,3),2)"), 2ULL, "max(min(1,3),2)");
check_eq(eu("min(max(2,5),max(1,3))"), 3ULL, "min(max(2,5),max(1,3))");
}
static void test_rotate_assign()
{
std::cout << "\n=== Rotate-Assign Operators ===\n";
MapContext ctx;
expr3u e;
bool ok;
ctx.vars["x"] = 1;
e.set_from_string("x<<<=3");
e.eval(&ok, &ctx);
check(ok, "x<<<=3 ok");
check_eq(ctx.vars["x"], 8ULL, "x=1; x<<<=3 == 8");
ctx.vars["x"] = 1;
e.set_from_string("x>>>=1");
e.eval(&ok, &ctx);
check(ok, "x>>>=1 ok");
check_eq(ctx.vars["x"], 0x8000000000000000ULL, "x=1; x>>>=1 == MSB");
}
static void test_assign_div_by_zero()
{
std::cout << "\n=== Assign Op Division by Zero ===\n";
MapContext ctx;
expr3u e;
bool ok;
ctx.vars["x"] = 10;
e.set_from_string("x/=0");
e.eval(&ok, &ctx);
check(!ok, "integer x/=0 returns error");
ctx.vars["x"] = 10;
e.set_from_string("x%=0");
e.eval(&ok, &ctx);
check(!ok, "integer x%=0 returns error");
}
// BUG: max/min return Token(false)==Token(0) (valid Number!) instead of an error
static void test_function_wrong_arity()
{
std::cout << "\n=== Function Wrong Arity (Bug: silently returns 0) ===\n";
bool ok;
eu("max(1)", &ok);
check(!ok, "max(1) returns error");
eu("min(1)", &ok);
check(!ok, "min(1) returns error");
eu("max()", &ok);
check(!ok, "max() returns error");
}
// BUG: float %=3.1 casts both operands to uint64_t and uses integer %, not fmod
static void test_float_assign_remainder()
{
std::cout << "\n=== Float Assign Remainder (Bug: int% instead of fmod) ===\n";
FloatMapCtx ctx;
ctx.vars["x"] = 5.3;
expr3f e("x%=3.1");
bool ok;
e.eval(&ok, &ctx);
check(ok, "float x%=3.1 ok");
check_near(ctx.vars["x"], std::fmod(5.3, 3.1), "float x%=3.1 uses fmod");
}
// BUG: <= and >= are right-associative in create_from_type, but < and > are left-associative
static void test_comparison_associativity()
{
std::cout << "\n=== Comparison Associativity (Bug: <= and >= are right-assoc) ===\n";
// C++: 3<=4<=5 is (3<=4)<=5 = 1<=5 = 1
check_eq(eu("3<=4<=5"), 1ULL, "3<=4<=5 == 1 (left-assoc)");
// C++: 5>=4>=3 is (5>=4)>=3 = 1>=3 = 0
check_eq(eu("5>=4>=3"), 0ULL, "5>=4>=3 == 0 (left-assoc)");
}
static void test_unary_plus()
{
std::cout << "\n=== Unary + ===\n";
// unary + works as 0+x via the binary + path
check_eq(eu("+5"), 5ULL, "+5");
check_eq(eu("+0"), 0ULL, "+0");
check_near(ef("+3.5"), 3.5, "+3.5 (float)");
}
// The tokenizer splits "2.0e-1" into tokens ["2.0e", -, "1"] at the '-' (remaining limitation).
static void test_scientific_notation()
{
std::cout << "\n=== Scientific Notation ===\n";
// positive-exponent forms work
check_near(ef("1.0e2"), 100.0, "1.0e2 == 100.0");
check_near(ef("1.5e2"), 150.0, "1.5e2 == 150.0");
// Bug #2 fixed: "1e10" (no dot) now recognized as sci-notation, not hex 0x1e10
check_near(ef("1e10"), 1e10, "\"1e10\" parsed as sci-notation (fixed)");
check_eq(eu("1e10"), 10000000000ULL, "eu(\"1e10\") == 10^10 as integer (fixed)");
// negative-exponent still broken: tokenizer splits "2.0e-1" at '-'
check_near(ef("2.0e-1"), -1.0, "2.0e-1 tokenized as (2.0e)-(1) == -1 (tokenizer limitation)");
}
// BUG #2: is_double() requires '.' in the string; pure scientific notation ("1e10") returns false
static void test_is_double_sci_notation()
{
std::cout << "\n=== is_double: scientific notation without dot (Bug) ===\n";
Token t_nodot(Token::Type::Number, "1e10");
check_eq(t_nodot.is_double(), true, "is_double(\"1e10\") should be true"); // BUG: returns false
// forms with a dot already work
Token t_dot(Token::Type::Number, "1.0e10");
check_eq(t_dot.is_double(), true, "is_double(\"1.0e10\") == true (has dot)");
}
// BUG #1 (fixed): double_as_str now uses max_digits10; intermediate precision is preserved
static void test_float_precision_loss()
{
std::cout << "\n=== Float Intermediate Precision (fixed) ===\n";
check_near(ef("1.0/3.0*3.0"), 1.0, "1.0/3.0*3.0 == 1.0 (precision preserved)");
}
static void test_float_comparisons()
{
std::cout << "\n=== Float Comparisons ===\n";
check_near(ef("1.5==1.5"), 1.0, "1.5==1.5");
check_near(ef("1.5==2.5"), 0.0, "1.5==2.5");
check_near(ef("1.5!=2.5"), 1.0, "1.5!=2.5");
check_near(ef("1.5!=1.5"), 0.0, "1.5!=1.5");
check_near(ef("1.5<2.5"), 1.0, "1.5<2.5");
check_near(ef("2.5<1.5"), 0.0, "2.5<1.5");
check_near(ef("1.5<=1.5"), 1.0, "1.5<=1.5");
check_near(ef("1.5<=2.5"), 1.0, "1.5<=2.5");
check_near(ef("2.5<=1.5"), 0.0, "2.5<=1.5");
check_near(ef("2.5>1.5"), 1.0, "2.5>1.5");
check_near(ef("1.5>=1.5"), 1.0, "1.5>=1.5");
check_near(ef("2.5>=1.5"), 1.0, "2.5>=1.5");
}
static void test_float_logical()
{
std::cout << "\n=== Float Logical && / || ===\n";
check_near(ef("1.5&&1.0"), 1.0, "1.5&&1.0");
check_near(ef("1.5&&0.0"), 0.0, "1.5&&0.0");
check_near(ef("0.0&&1.5"), 0.0, "0.0&&1.5");
check_near(ef("0.0||0.0"), 0.0, "0.0||0.0");
check_near(ef("0.0||1.5"), 1.0, "0.0||1.5");
check_near(ef("1.5||0.0"), 1.0, "1.5||0.0");
}
// BUG #5: unary use of binary-only operators silently returns 0 instead of an error
static void test_unary_invalid()
{
std::cout << "\n=== Unary Misuse of Binary Operators (Bug: silent 0) ===\n";
bool ok;
eu("*5", &ok);
check(!ok, "unary * is error");
eu("/5", &ok);
check(!ok, "unary / is error");
eu("%5", &ok);
check(!ok, "unary % is error");
}
static void test_chained_assignment()
{
std::cout << "\n=== Chained Assignment (right-assoc) ===\n";
MapContext ctx;
ctx.vars["x"] = 0;
ctx.vars["y"] = 0;
expr3u e;
bool ok;
e.set_from_string("x=y=5");
e.eval(&ok, &ctx);
check(ok, "x=y=5 ok");
check_eq(ctx.vars["y"], 5ULL, "x=y=5: y==5");
check_eq(ctx.vars["x"], 5ULL, "x=y=5: x==5");
}
static void test_comparison_chain()
{
std::cout << "\n=== Comparison Chain (left-assoc) ===\n";
// (1<2)<3 = 1<3 = 1
check_eq(eu("1<2<3"), 1ULL, "1<2<3 == 1");
// (3>2)>1 = 1>1 = 0
check_eq(eu("3>2>1"), 0ULL, "3>2>1 == 0");
// (1==1)==1 = 1==1 = 1
check_eq(eu("1==1==1"), 1ULL, "1==1==1 == 1");
}
static void test_float_assign_ops()
{
std::cout << "\n=== Float Assign Operators ===\n";
FloatMapCtx ctx;
expr3f e;
bool ok;
#define FASSIGN_TEST(init, expr_str, expected, label) \
ctx.vars["x"] = (init); \
e.set_from_string(expr_str); \
e.eval(&ok, &ctx); \
check(ok, label " ok"); \
check_near(ctx.vars["x"], (expected), label)
FASSIGN_TEST(1.5, "x+=1.0", 2.5, "float x+=1.0");
FASSIGN_TEST(3.5, "x-=1.0", 2.5, "float x-=1.0");
FASSIGN_TEST(2.0, "x*=1.5", 3.0, "float x*=1.5");
FASSIGN_TEST(6.0, "x/=2.0", 3.0, "float x/=2.0");
#undef FASSIGN_TEST
}
static void test_large_shift()
{
std::cout << "\n=== Large Shift (boundary) ===\n";
// shift by 63: well-defined for uint64_t
check_eq(eu("1<<3f"), 0x8000000000000000ULL, "1<<63 (0x3f)");
check_eq(eu("0x8000000000000000>>3f"), 1ULL, "MSB>>63 == 1");
// NOTE: shift by >= 64 is UB in C++; no test asserts a specific value there
}
static void test_float_bitwise()
{
std::cout << "\n=== Float Bitwise Ops ===\n";
// ~x on float: truncates to uint64, bit-inverts, casts back to double
double expected_bitnot = static_cast<double>(~static_cast<uint64_t>(1.5));
check_near(ef("~1.5"), expected_bitnot, "~1.5 truncates to uint64 then bit-not");
// & | ^ on floats also truncate to uint64 first
check_near(ef("0xF0|0x0F"), static_cast<double>(0xFF), "float 0xF0|0x0F == 0xFF");
check_near(ef("0xFF&0x0F"), static_cast<double>(0x0F), "float 0xFF&0x0F == 0x0F");
}
static void test_function_with_vars()
{
std::cout << "\n=== Function Args as Variables ===\n";
// NOTE: single-char hex names ("a".."f") are parsed as integer literals (0xa..0xf),
// not as variable names. Use names outside the hex range.
MapWithFuncsContext ctx;
ctx.vars["p"] = 3;
ctx.vars["q"] = 7;
expr3u e;
bool ok;
e.set_from_string("max(p,q)");
check_eq(e.eval(&ok, &ctx), 7ULL, "max(p,q) where p=3, q=7");
check(ok, "max(p,q) ok");
e.set_from_string("min(p,q)");
check_eq(e.eval(&ok, &ctx), 3ULL, "min(p,q) where p=3, q=7");
check(ok, "min(p,q) ok");
e.set_from_string("max(p,max(q,5))");
check_eq(e.eval(&ok, &ctx), 7ULL, "max(p,max(q,5)) == 7");
}
static void test_nested_parens()
{
std::cout << "\n=== Deeply Nested Parens ===\n";
check_eq(eu("((((1+2))))"), 3ULL, "((((1+2))))");
check_eq(eu("((2+3)*(4+5))"), 45ULL, "((2+3)*(4+5))");
}
// StrConstant is pushed through the RPN pipeline so functions can receive string args.
static void test_string_literal()
{
std::cout << "\n=== String Literal Handling ===\n";
expr3u e;
bool ok;
// bare string: parses fine, but eval returns success=false (not a Number)
Token r = e.set_from_string("\"hello\"");
check(!r.is_error(), "\"hello\" parses without error");
e.eval(&ok);
check(!ok, "\"hello\" eval success=false (not a number)");
// string as function argument: StrConstant reaches exec_function intact
class StrFuncCtx : public expr_eval_context {
public:
std::string received;
Token resolve_var_if_needed(const Token& t) override { return t; }
bool assign(const Token&, const Token&) override { return true; }
Token exec_function(const Token& func, std::vector<Token>& args) override {
if (func.str == "id" && !args.empty() && args[0].is_string()) {
received = args[0].str;
return Token::make_constant(1);
}
return {};
}
} ctx;
e.set_from_string("id(\"world\")");
e.eval(&ok, &ctx);
check(ok, "id(\"world\") ok");
check_eq(ctx.received, std::string("world"), "StrConstant arg reached exec_function");
}
// ---------------------------------------------------------------------------
int main()
{
std::cout << "expr3 Test Suite\n================";
test_arithmetic();
test_bitwise();
test_shifts_and_rotates();
test_large_shift();
test_unary_integer();
test_unary_plus();
test_unary_chain();
test_unary_invalid();
test_unary_float();
test_float_bitwise();
test_logical();
test_float_logical();
test_comparisons();
test_comparison_associativity();
test_comparison_chain();
test_float_arithmetic();
test_float_comparisons();
test_float_precision_loss();
test_float_assign_remainder();
test_float_assign_ops();
test_functions();
test_nested_functions();
test_function_wrong_arity();
test_function_with_vars();
test_variables();
test_assignment_ops();
test_chained_assignment();
test_rotate_assign();
test_signed_arithmetic();
test_str_as_double();
test_is_double_sci_notation();
test_scientific_notation();
test_division_by_zero();
test_assign_div_by_zero();
test_set_from_string_return();
test_is_integer_base();
test_error_handling();
test_nested_parens();
test_string_literal();
test_intermediate_repr();
std::cout << "\n================\nResults:\n"
<< " pass: " << g_pass << "\n"
<< " fail: " << g_fail << "\n";
return (g_fail > 0) ? 1 : 0;
}