aboutsummaryrefslogtreecommitdiff
path: root/tests/test_core.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_core.py')
-rw-r--r--tests/test_core.py6201
1 files changed, 599 insertions, 5602 deletions
diff --git a/tests/test_core.py b/tests/test_core.py
index b3e57490..f8b077b7 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -10,103 +10,29 @@ class T(RunnerCore): # Short name, to make it more fun to use manually on the co
return not ('i386-pc-linux-gnu' in COMPILER_OPTS or self.env.get('EMCC_LLVM_TARGET') == 'i386-pc-linux-gnu')
def test_hello_world(self):
- src = '''
- #include <stdio.h>
- int main()
- {
- printf("hello, world!\\n");
- return 0;
- }
- '''
- self.do_run(src, 'hello, world!')
+ test_path = path_from_root('tests', 'core', 'test_hello_world')
+ src, output = (test_path + s for s in ('.in', '.out'))
+
+ self.do_run_from_file(src, output)
assert 'EMSCRIPTEN_GENERATED_FUNCTIONS' not in open(self.in_dir('src.cpp.o.js')).read(), 'must not emit this unneeded internal thing'
def test_intvars(self):
if self.emcc_args == None: return self.skip('needs ta2')
- src = '''
- #include <stdio.h>
- int global = 20;
- int *far;
- int main()
- {
- int x = 5;
- int y = x+17;
- int z = (y-1)/2; // Should stay an integer after division!
- y += 1;
- int w = x*3+4;
- int k = w < 15 ? 99 : 101;
- far = &k;
- *far += global;
- int i = k > 100; // Should be an int, not a bool!
- int j = i << 6;
- j >>= 1;
- j = j ^ 5;
- int h = 1;
- h |= 0;
- int p = h;
- p &= 0;
- printf("*%d,%d,%d,%d,%d,%d,%d,%d,%d*\\n", x, y, z, w, k, i, j, h, p);
-
- long hash = -1;
- size_t perturb;
- int ii = 0;
- for (perturb = hash; ; perturb >>= 5) {
- printf("%d:%d", ii, perturb);
- ii++;
- if (ii == 9) break;
- printf(",");
- }
- printf("*\\n");
- printf("*%.1d,%.2d*\\n", 56, 9);
-
- // Fixed-point math on 64-bit ints. Tricky to support since we have no 64-bit shifts in JS
- {
- struct Fixed {
- static int Mult(int a, int b) {
- return ((long long)a * (long long)b) >> 16;
- }
- };
- printf("fixed:%d\\n", Fixed::Mult(150000, 140000));
- }
+ test_path = path_from_root('tests', 'core', 'test_intvars')
+ src, output = (test_path + s for s in ('.in', '.out'))
- printf("*%ld*%p\\n", (long)21, &hash); // The %p should not enter an infinite loop!
- return 0;
- }
- '''
- self.do_run(src, '*5,23,10,19,121,1,37,1,0*\n0:-1,1:134217727,2:4194303,3:131071,4:4095,5:127,6:3,7:0,8:0*\n*56,09*\nfixed:320434\n*21*')
+ self.do_run_from_file(src, output)
def test_sintvars(self):
Settings.CORRECT_SIGNS = 1 # Relevant to this test
- src = '''
- #include <stdio.h>
- struct S {
- char *match_start;
- char *strstart;
- };
- int main()
- {
- struct S _s;
- struct S *s = &_s;
- unsigned short int sh;
-
- s->match_start = (char*)32522;
- s->strstart = (char*)(32780);
- printf("*%d,%d,%d*\\n", (int)s->strstart, (int)s->match_start, (int)(s->strstart - s->match_start));
- sh = s->strstart - s->match_start;
- printf("*%d,%d*\\n", sh, sh>>7);
-
- s->match_start = (char*)32999;
- s->strstart = (char*)(32780);
- printf("*%d,%d,%d*\\n", (int)s->strstart, (int)s->match_start, (int)(s->strstart - s->match_start));
- sh = s->strstart - s->match_start;
- printf("*%d,%d*\\n", sh, sh>>7);
- }
- '''
- output = '*32780,32522,258*\n*258,2*\n*32780,32999,-219*\n*65317,510*'
Settings.CORRECT_OVERFLOWS = 0 # We should not need overflow correction to get this right
- self.do_run(src, output, force_c=True)
+
+ test_path = path_from_root('tests', 'core', 'test_sintvars')
+ src, output = (test_path + s for s in ('.in', '.out'))
+
+ self.do_run_from_file(src, output, force_c=True)
def test_i64(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('i64 mode 1 requires ta2')
@@ -338,169 +264,42 @@ class T(RunnerCore): # Short name, to make it more fun to use manually on the co
def test_i64_b(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
- src = r'''
- #include <stdio.h>
- #include <sys/time.h>
-
- typedef long long int64;
-
- #define PRMJ_USEC_PER_SEC 1000000L
-
- int main(int argc, char * argv[]) {
- int64 sec = 1329409675 + argc;
- int64 usec = 2329509675;
- int64 mul = int64(sec) * PRMJ_USEC_PER_SEC;
- int64 add = mul + int64(usec);
- int add_low = add;
- int add_high = add >> 32;
- printf("*%lld,%lld,%u,%u*\n", mul, add, add_low, add_high);
- int64 x = sec + (usec << 25);
- x >>= argc*3;
- printf("*%llu*\n", x);
- return 0;
- }
- '''
+ test_path = path_from_root('tests', 'core', 'test_i64_b')
+ src, output = (test_path + s for s in ('.in', '.out'))
- self.do_run(src, '*1329409676000000,1329412005509675,3663280683,309527*\n*9770671914067409*\n')
+ self.do_run_from_file(src, output)
def test_i64_cmp(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
- src = r'''
- #include <stdio.h>
-
- typedef long long int64;
-
- bool compare(int64 val) {
- return val == -12;
- }
-
- bool compare2(int64 val) {
- return val < -12;
- }
-
- int main(int argc, char * argv[]) {
- printf("*%d,%d,%d,%d,%d,%d*\n", argc, compare(argc-1-12), compare(1000+argc), compare2(argc-1-10), compare2(argc-1-14), compare2(argc+1000));
- return 0;
- }
- '''
+ test_path = path_from_root('tests', 'core', 'test_i64_cmp')
+ src, output = (test_path + s for s in ('.in', '.out'))
- self.do_run(src, '*1,1,0,0,1,0*\n')
+ self.do_run_from_file(src, output)
def test_i64_cmp2(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
- src = r'''
- #include <inttypes.h>
- #include <stdio.h>
-
- typedef int32_t INT32;
- typedef int64_t INT64;
- typedef uint8_t UINT8;
+ test_path = path_from_root('tests', 'core', 'test_i64_cmp2')
+ src, output = (test_path + s for s in ('.in', '.out'))
- void interface_clock_changed()
- {
- UINT8 m_divshift;
- INT32 m_divisor;
-
- //INT64 attos = m_attoseconds_per_cycle;
- INT64 attos = 279365114840;
- m_divshift = 0;
- while (attos >= (1UL << 31))
- {
- m_divshift++;
- printf("m_divshift is %i, on %Ld >?= %lu\n", m_divshift, attos, 1UL << 31);
- attos >>= 1;
- }
- m_divisor = attos;
-
- printf("m_divisor is %i\n",m_divisor);
- }
-
- int main() {
- interface_clock_changed();
- return 0;
- }
- '''
- self.do_run(src, '''m_divshift is 1, on 279365114840 >?= 2147483648
-m_divshift is 2, on 139682557420 >?= 2147483648
-m_divshift is 3, on 69841278710 >?= 2147483648
-m_divshift is 4, on 34920639355 >?= 2147483648
-m_divshift is 5, on 17460319677 >?= 2147483648
-m_divshift is 6, on 8730159838 >?= 2147483648
-m_divshift is 7, on 4365079919 >?= 2147483648
-m_divshift is 8, on 2182539959 >?= 2147483648
-m_divisor is 1091269979
-''')
+ self.do_run_from_file(src, output)
def test_i64_double(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
+ test_path = path_from_root('tests', 'core', 'test_i64_double')
+ src, output = (test_path + s for s in ('.in', '.out'))
- src = r'''
- #include <stdio.h>
-
- typedef long long int64;
- #define JSDOUBLE_HI32_SIGNBIT 0x80000000
-
- bool JSDOUBLE_IS_NEGZERO(double d)
- {
- union {
- struct {
- unsigned int lo, hi;
- } s;
- double d;
- } x;
- if (d != 0)
- return false;
- x.d = d;
- return (x.s.hi & JSDOUBLE_HI32_SIGNBIT) != 0;
- }
-
- bool JSINT64_IS_NEGZERO(int64 l)
- {
- union {
- int64 i;
- double d;
- } x;
- if (l != 0)
- return false;
- x.i = l;
- return x.d == -0;
- }
-
- int main(int argc, char * argv[]) {
- printf("*%d,%d,%d,%d*\n", JSDOUBLE_IS_NEGZERO(0), JSDOUBLE_IS_NEGZERO(-0), JSDOUBLE_IS_NEGZERO(-1), JSDOUBLE_IS_NEGZERO(+1));
- printf("*%d,%d,%d,%d*\n", JSINT64_IS_NEGZERO(0), JSINT64_IS_NEGZERO(-0), JSINT64_IS_NEGZERO(-1), JSINT64_IS_NEGZERO(+1));
- return 0;
- }
- '''
- self.do_run(src, '*0,0,0,0*\n*1,1,0,0*\n') # same as gcc
+ self.do_run_from_file(src, output)
def test_i64_umul(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
- src = r'''
- #include <inttypes.h>
- #include <stdio.h>
-
- typedef uint32_t UINT32;
- typedef uint64_t UINT64;
+ test_path = path_from_root('tests', 'core', 'test_i64_umul')
+ src, output = (test_path + s for s in ('.in', '.out'))
- int main() {
- volatile UINT32 testu32a = 2375724032U;
- UINT32 bigu32 = 0xffffffffU;
- volatile UINT64 testu64a = 14746250828952703000U;
-
- while ((UINT64)testu32a * (UINT64)bigu32 < testu64a) {
- printf("testu64a is %llu\n", testu64a);
- testu64a /= 2;
- }
-
- return 0;
- }
- '''
- self.do_run(src, 'testu64a is 14746250828952703000\n')
+ self.do_run_from_file(src, output)
def test_i64_precise(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
@@ -579,513 +378,117 @@ m_divisor is 1091269979
def test_i64_llabs(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
Settings.PRECISE_I64_MATH = 2
- self.do_run(r'''
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char ** argv) {
- printf("%lld,%lld\n", llabs(-576460752303423489), llabs(576460752303423489));
- return 0;
- }
- ''', '576460752303423489,576460752303423489')
+ test_path = path_from_root('tests', 'core', 'test_i64_llabs')
+ src, output = (test_path + s for s in ('.in', '.out'))
+
+ self.do_run_from_file(src, output)
def test_i64_zextneg(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
- src = r'''
- #include <stdint.h>
- #include <stdio.h>
+ test_path = path_from_root('tests', 'core', 'test_i64_zextneg')
+ src, output = (test_path + s for s in ('.in', '.out'))
- int main(int argc, char *argv[])
- {
- uint8_t byte = 0x80;
- uint16_t two = byte;
- uint32_t four = byte;
- uint64_t eight = byte;
-
- printf("value: %d,%d,%d,%lld.\n", byte, two, four, eight);
-
- return 0;
- }
- '''
- self.do_run(src, 'value: 128,128,128,128.')
+ self.do_run_from_file(src, output)
def test_i64_7z(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
- src = r'''
- #include <stdint.h>
- #include <stdio.h>
- uint64_t a, b;
- int main(int argc, char *argv[])
- {
- a = argc;
- b = argv[1][0];
- printf("%d,%d\n", a, b);
- if (a > a + b || a > a + b + 1) {
- printf("one %lld, %lld", a, b);
- return 0;
- }
- printf("zero %lld, %lld", a, b);
- return 0;
- }
- '''
- self.do_run(src, 'zero 2, 104', ['hallo'])
+ test_path = path_from_root('tests', 'core', 'test_i64_7z')
+ src, output = (test_path + s for s in ('.in', '.out'))
+
+ self.do_run_from_file(src, output, ['hallo'])
def test_i64_i16(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
- src = r'''
- #include <stdint.h>
- #include <stdio.h>
- int main(int argc, char ** argv){
- int y=-133;
- int64_t x= ((int64_t)((short)(y)))*(100 + argc);
- if(x>0)
- printf(">0\n");
- else
- printf("<=0\n");
- }
- '''
- self.do_run(src, '<=0')
+ test_path = path_from_root('tests', 'core', 'test_i64_i16')
+ src, output = (test_path + s for s in ('.in', '.out'))
+
+ self.do_run_from_file(src, output)
def test_i64_qdouble(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
- src = r'''
- #include <stdio.h>
- typedef long long qint64; /* 64 bit signed */
- typedef double qreal;
+ test_path = path_from_root('tests', 'core', 'test_i64_qdouble')
+ src, output = (test_path + s for s in ('.in', '.out'))
-
- int main(int argc, char **argv)
- {
- qreal c = 111;
- qint64 d = -111 + (argc - 1);
- c += d;
- if (c < -1 || c > 1)
- {
- printf("Failed!\n");
- }
- else
- {
- printf("Succeeded!\n");
- }
- };
- '''
- self.do_run(src, 'Succeeded!')
+ self.do_run_from_file(src, output)
def test_i64_varargs(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2')
- src = r'''
- #include <stdio.h>
- #include <stdint.h>
- #include <stdarg.h>
-
- int64_t ccv_cache_generate_signature(char *msg, int len, int64_t sig_start, ...) {
- if (sig_start < 10123)
- printf("%s\n", msg+len);
- va_list v;
- va_start(v, sig_start);
- if (sig_start > 1413)
- printf("%d\n", va_arg(v, int));
- else
- printf("nada\n");
- va_end(v);
- return len*sig_start*(msg[0]+1);
- }
+ test_path = path_from_root('tests', 'core', 'test_i64_varargs')
+ src, output = (test_path + s for s in ('.in', '.out'))
- int main(int argc, char **argv)
- {
- for (int i = 0; i < argc; i++) {
- int64_t x;
- if (i % 123123 == 0)
- x = ccv_cache_generate_signature(argv[i], i+2, (int64_t)argc*argc, 54.111);
- else
- x = ccv_cache_generate_signature(argv[i], i+2, (int64_t)argc*argc, 13);
- printf("%lld\n", x);
- }
- };
- '''
- self.do_run(src, '''in/this.program
-nada
-1536
-a
-nada
-5760
-fl
-nada
-6592
-sdfasdfasdf
-nada
-7840
-''', 'waka fleefl asdfasdfasdfasdf'.split(' '))
+ self.do_run_from_file(src, output, 'waka fleefl asdfasdfasdfasdf'.split(' '))
def test_i32_mul_precise(self):
if self.emcc_args == None: return self.skip('needs ta2')
- src = r'''
- #include <stdio.h>
+ test_path = path_from_root('tests', 'core', 'test_i32_mul_precise')
+ src, output = (test_path + s for s in ('.in', '.out'))
- int main(int argc, char **argv) {
- unsigned long d1 = 0x847c9b5d;
- unsigned long q = 0x549530e1;
- if (argc > 1000) { q += argc; d1 -= argc; } // confuse optimizer
- printf("%lu\n", d1*q);
- return 0;
- }
- '''
- self.do_run(src, '3217489085')
+ self.do_run_from_file(src, output)
def test_i32_mul_semiprecise(self):
if Settings.ASM_JS: return self.skip('asm is always fully precise')
Settings.PRECISE_I32_MUL = 0 # we want semiprecise here
- src = r'''
- #include <stdio.h>
-
- typedef unsigned int uint;
-
- // from cube2, zlib licensed
+ test_path = path_from_root('tests', 'core', 'test_i32_mul_semiprecise')
+ src, output = (test_path + s for s in ('.in', '.out'))
- #define N (624)
- #define M (397)
- #define K (0x9908B0DFU)
-
- static uint state[N];
- static int next = N;
-
- void seedMT(uint seed)
- {
- state[0] = seed;
- for(uint i = 1; i < N; i++) // if we do not do this precisely, at least we should coerce to int immediately, not wait
- state[i] = seed = 1812433253U * (seed ^ (seed >> 30)) + i;
- next = 0;
- }
-
- int main() {
- seedMT(5497);
- for (int i = 0; i < 10; i++) printf("%d: %u\n", i, state[i]);
- return 0;
- }
- '''
- self.do_run(src, '''0: 5497
-1: 2916432318
-2: 2502517762
-3: 3151524867
-4: 2323729668
-5: 2053478917
-6: 2409490438
-7: 848473607
-8: 691103752
-9: 3915535113
-''')
+ self.do_run_from_file(src, output)
def test_i16_emcc_intrinsic(self):
Settings.CORRECT_SIGNS = 1 # Relevant to this test
- src = r'''
- #include <stdio.h>
-
- int test(unsigned short a, unsigned short b) {
- unsigned short result = a;
- result += b;
- if (result < b) printf("C!");
- return result;
- }
+ test_path = path_from_root('tests', 'core', 'test_i16_emcc_intrinsic')
+ src, output = (test_path + s for s in ('.in', '.out'))
- int main(void) {
- printf(",%d,", test(0, 0));
- printf(",%d,", test(1, 1));
- printf(",%d,", test(65535, 1));
- printf(",%d,", test(1, 65535));
- printf(",%d,", test(32768, 32767));
- printf(",%d,", test(32768, 32768));
- return 0;
- }
- '''
- self.do_run(src, ',0,,2,C!,0,C!,0,,65535,C!,0,')
+ self.do_run_from_file(src, output)
def test_double_i64_conversion(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2')
- src = r'''
- #include <cassert>
- #include <inttypes.h>
- #include <stdio.h>
+ test_path = path_from_root('tests', 'core', 'test_double_i64_conversion')
+ src, output = (test_path + s for s in ('.in', '.out'))
- __attribute((noinline)) bool eq(double d, int64_t i) {
- int64_t i2 = (int64_t)d;
- if (i != i2) {
- printf("%.20g converted to int64 returns %lld, not %lld as expected!\n", d, i2, i);
- }
- return i == i2;
- }
-
- int main() {
- assert(eq(0.0, 0));
- assert(eq(-0.0, 0));
- assert(eq(0.1, 0));
- assert(eq(-0.1, 0));
- assert(eq(0.6, 0));
- assert(eq(-0.6, 0));
- assert(eq(1.0, 1));
- assert(eq(-1.0, -1));
- assert(eq(1.1, 1));
- assert(eq(-1.1, -1));
- assert(eq(1.6, 1));
- assert(eq(-1.6, -1));
- assert(eq(4294967295.0, 4294967295LL));
- assert(eq(4294967295.5, 4294967295LL));
- assert(eq(4294967296.0, 4294967296LL));
- assert(eq(4294967296.5, 4294967296LL));
- assert(eq(14294967295.0, 14294967295LL));
- assert(eq(14294967295.5, 14294967295LL));
- assert(eq(14294967296.0, 14294967296LL));
- assert(eq(14294967296.5, 14294967296LL));
- assert(eq(-4294967295.0, -4294967295LL));
- assert(eq(-4294967295.5, -4294967295LL));
- assert(eq(-4294967296.0, -4294967296LL));
- assert(eq(-4294967296.5, -4294967296LL));
- assert(eq(-14294967295.0, -14294967295LL));
- assert(eq(-14294967295.5, -14294967295LL));
- assert(eq(-14294967296.0, -14294967296LL));
- assert(eq(-14294967296.5, -14294967296LL));
-
- assert(eq(4294967295.3, 4294967295LL));
- assert(eq(4294967296.3, 4294967296LL));
- assert(eq(14294967295.3, 14294967295LL));
- assert(eq(14294967296.3, 14294967296LL));
- assert(eq(-4294967295.3, -4294967295LL));
- assert(eq(-4294967296.3, -4294967296LL));
- assert(eq(-14294967295.3, -14294967295LL));
- assert(eq(-14294967296.3, -14294967296LL));
-
- assert(eq(4294967295.8, 4294967295LL));
- assert(eq(4294967296.8, 4294967296LL));
- assert(eq(14294967295.8, 14294967295LL));
- assert(eq(14294967296.8, 14294967296LL));
- assert(eq(-4294967295.8, -4294967295LL));
- assert(eq(-4294967296.8, -4294967296LL));
- assert(eq(-14294967295.8, -14294967295LL));
- assert(eq(-14294967296.8, -14294967296LL));
-
- // The following number is the largest double such that all integers smaller than this can exactly be represented in a double.
- assert(eq(9007199254740992.0, 9007199254740992LL /* == 2^53 */));
- assert(eq(-9007199254740992.0, -9007199254740992LL /* == -2^53 */));
-
- printf("OK!\n");
- return 0;
- }
- '''
- self.do_run(src, 'OK!\n');
+ self.do_run_from_file(src, output)
def test_float32_precise(self):
Settings.PRECISE_F32 = 1
- src = r'''
- #include <stdio.h>
+ test_path = path_from_root('tests', 'core', 'test_float32_precise')
+ src, output = (test_path + s for s in ('.in', '.out'))
- int main(int argc, char **argv) {
- float x = 1.23456789123456789;
- float y = 5.20456089123406709;
- while (argc > 10 || argc % 19 == 15) {
- // confuse optimizer
- x /= y;
- y = 2*y - 1;
- argc--;
- }
- x = x - y;
- y = 3*y - x/2;
- x = x*y;
- y += 0.000000000123123123123;
- x -= y/7.654;
- printf("\n%.20f, %.20f\n", x, y);
- return 0;
- }
- '''
- self.do_run(src, '\n-72.16590881347656250000, 17.59867858886718750000\n')
+ self.do_run_from_file(src, output)
def test_negative_zero(self):
- src = r'''
- #include <stdio.h>
- #include <math.h>
+ test_path = path_from_root('tests', 'core', 'test_negative_zero')
+ src, output = (test_path + s for s in ('.in', '.out'))
- int main() {
- #define TEST(x, y) \
- printf("%.2f, %.2f ==> %.2f\n", x, y, copysign(x, y));
- TEST( 5.0f, 5.0f);
- TEST( 5.0f, -5.0f);
- TEST(-5.0f, 5.0f);
- TEST(-5.0f, -5.0f);
- TEST( 5.0f, 4.0f);
- TEST( 5.0f, -4.0f);
- TEST(-5.0f, 4.0f);
- TEST(-5.0f, -4.0f);
- TEST( 0.0f, 5.0f);
- TEST( 0.0f, -5.0f);
- TEST(-0.0f, 5.0f);
- TEST(-0.0f, -5.0f);
- TEST( 5.0f, 0.0f);
- TEST( 5.0f, -0.0f);
- TEST(-5.0f, 0.0f);
- TEST(-5.0f, -0.0f);
- TEST( 0.0f, 0.0f);
- TEST( 0.0f, -0.0f);
- TEST(-0.0f, 0.0f);
- TEST(-0.0f, -0.0f);
- return 0;
- }
- '''
- self.do_run(src, '''5.00, 5.00 ==> 5.00
-5.00, -5.00 ==> -5.00
--5.00, 5.00 ==> 5.00
--5.00, -5.00 ==> -5.00
-5.00, 4.00 ==> 5.00
-5.00, -4.00 ==> -5.00
--5.00, 4.00 ==> 5.00
--5.00, -4.00 ==> -5.00
-0.00, 5.00 ==> 0.00
-0.00, -5.00 ==> -0.00
--0.00, 5.00 ==> 0.00
--0.00, -5.00 ==> -0.00
-5.00, 0.00 ==> 5.00
-5.00, -0.00 ==> -5.00
--5.00, 0.00 ==> 5.00
--5.00, -0.00 ==> -5.00
-0.00, 0.00 ==> 0.00
-0.00, -0.00 ==> -0.00
--0.00, 0.00 ==> 0.00
--0.00, -0.00 ==> -0.00
-''')
+ self.do_run_from_file(src, output)
def test_llvm_intrinsics(self):
if self.emcc_args == None: return self.skip('needs ta2')
Settings.PRECISE_I64_MATH = 2 # for bswap64
- src = r'''
- #include <stdio.h>
- #include <sys/types.h>
-
- extern "C" {
- extern unsigned short llvm_bswap_i16(unsigned short x);
- extern unsigned int llvm_bswap_i32(unsigned int x);
- extern int32_t llvm_ctlz_i32(int32_t x);
- extern int64_t llvm_ctlz_i64(int64_t x);
- extern int32_t llvm_cttz_i32(int32_t x);
- extern int64_t llvm_cttz_i64(int64_t x);
- extern int32_t llvm_ctpop_i32(int32_t x);
- extern int64_t llvm_ctpop_i64(int64_t x);
- extern int llvm_expect_i32(int x, int y);
- }
-
- int main(void) {
- unsigned short x = 0xc8ef;
- printf("%x,%x\n", x&0xff, x >> 8);
- x = llvm_bswap_i16(x);
- printf("%x,%x\n", x&0xff, x >> 8);
-
- unsigned int y = 0xc5de158a;
- printf("%x,%x,%x,%x\n", y&0xff, (y>>8)&0xff, (y>>16)&0xff, (y>>24)&0xff);
- y = llvm_bswap_i32(y);
- printf("%x,%x,%x,%x\n", y&0xff, (y>>8)&0xff, (y>>16)&0xff, (y>>24)&0xff);
-
- printf("%d,%d\n", (int)llvm_ctlz_i64(((int64_t)1) << 40), llvm_ctlz_i32(1<<10));
- printf("%d,%d\n", (int)llvm_cttz_i64(((int64_t)1) << 40), llvm_cttz_i32(1<<10));
- printf("%d,%d\n", (int)llvm_ctpop_i64((0x3101ULL << 32) | 1), llvm_ctpop_i32(0x3101));
- printf("%d\n", (int)llvm_ctpop_i32(-594093059));
-
- printf("%d\n", llvm_expect_i32(x % 27, 3));
+ test_path = path_from_root('tests', 'core', 'test_llvm_intrinsics')
+ src, output = (test_path + s for s in ('.in', '.out'))
- int64_t a = 1;
- a = __builtin_bswap64(a);
- printf("%lld\n", a);
-
- return 0;
- }
- '''
- self.do_run(src, '''ef,c8
-c8,ef
-8a,15,de,c5
-c5,de,15,8a
-23,21
-40,10
-5,4
-22
-13
-72057594037927936
-''')
+ self.do_run_from_file(src, output)
def test_bswap64(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2')
- src = r'''
- #include <stdio.h>
- #include <stdlib.h>
-
- #include <iostream>
- #include <string>
- #include <sstream>
-
- typedef unsigned long long quint64;
+ test_path = path_from_root('tests', 'core', 'test_bswap64')
+ src, output = (test_path + s for s in ('.in', '.out'))
- using namespace std;
-
- inline quint64 qbswap(quint64 source)
- {
- return 0
- | ((source & quint64(0x00000000000000ffLL)) << 56)
- | ((source & quint64(0x000000000000ff00LL)) << 40)
- | ((source & quint64(0x0000000000ff0000LL)) << 24)
- | ((source & quint64(0x00000000ff000000LL)) << 8)
- | ((source & quint64(0x000000ff00000000LL)) >> 8)
- | ((source & quint64(0x0000ff0000000000LL)) >> 24)
- | ((source & quint64(0x00ff000000000000LL)) >> 40)
- | ((source & quint64(0xff00000000000000LL)) >> 56);
- }
-
- int main()
- {
- quint64 v = strtoull("4433ffeeddccbb00", NULL, 16);
- printf("%lld\n", v);
-
- const string string64bitInt = "4433ffeeddccbb00";
- stringstream s(string64bitInt);
- quint64 int64bitInt = 0;
- printf("1\n");
- s >> hex >> int64bitInt;
- printf("2\n");
-
- stringstream out;
- out << hex << qbswap(int64bitInt);
-
- cout << out.str() << endl;
- cout << hex << int64bitInt << endl;
- cout << string64bitInt << endl;
-
- if (out.str() != "bbccddeeff3344")
- {
- cout << "Failed!" << endl;
- }
- else
- {
- cout << "Succeeded!" << endl;
- }
-
- return 0;
- }
- '''
- self.do_run(src, '''4914553019779824384
-1
-2
-bbccddeeff3344
-4433ffeeddccbb00
-4433ffeeddccbb00
-Succeeded!
-''')
+ self.do_run_from_file(src, output)
def test_sha1(self):
if self.emcc_args == None: return self.skip('needs ta2')
@@ -1351,240 +754,64 @@ Succeeded!
def test_bitfields(self):
if self.emcc_args is None: Settings.SAFE_HEAP = 0 # bitfields do loads on invalid areas, by design
- src = '''
- #include <stdio.h>
- struct bitty {
- unsigned x : 1;
- unsigned y : 1;
- unsigned z : 1;
- };
- int main()
- {
- bitty b;
- printf("*");
- for (int i = 0; i <= 1; i++)
- for (int j = 0; j <= 1; j++)
- for (int k = 0; k <= 1; k++) {
- b.x = i;
- b.y = j;
- b.z = k;
- printf("%d,%d,%d,", b.x, b.y, b.z);
- }
- printf("*\\n");
- return 0;
- }
- '''
- self.do_run(src, '*0,0,0,0,0,1,0,1,0,0,1,1,1,0,0,1,0,1,1,1,0,1,1,1,*')
- def test_floatvars(self):
- src = '''
- #include <stdio.h>
+ test_path = path_from_root('tests', 'core', 'test_bitfields')
+ src, output = (test_path + s for s in ('.in', '.out'))
- // headers test, see issue #1013
- #include<cfloat>
- #include<cmath>
+ self.do_run_from_file(src, output)
- int main(int argc, char **argv)
- {
- float x = 1.234, y = 3.5, q = 0.00000001;
- y *= 3;
- int z = x < y;
- printf("*%d,%d,%.1f,%d,%.4f,%.2f*\\n", z, int(y), y, (int)x, x, q);
-
- printf("%.2f, %.2f, %.2f, %.2f\\n", fmin(0.5, 3.3), fmin(NAN, 3.3), fmax(0.5, 3.3), fmax(NAN, 3.3));
-
- printf("small: %.10f\\n", argc * 0.000001);
-
- /*
- // Rounding behavior
- float fs[6] = { -2.75, -2.50, -2.25, 2.25, 2.50, 2.75 };
- double ds[6] = { -2.75, -2.50, -2.25, 2.25, 2.50, 2.75 };
- for (int i = 0; i < 6; i++)
- printf("*int(%.2f)=%d,%d*\\n", fs[i], int(fs[i]), int(ds[i]));
- */
+ def test_floatvars(self):
+ test_path = path_from_root('tests', 'core', 'test_floatvars')
+ src, output = (test_path + s for s in ('.in', '.out'))
- return 0;
- }
- '''
- self.do_run(src, '*1,10,10.5,1,1.2340,0.00*\n0.50, 3.30, 3.30, 3.30\nsmall: 0.0000010000\n')
+ self.do_run_from_file(src, output)
def test_fast_math(self):
if self.emcc_args is None: return self.skip('requires emcc')
Building.COMPILER_TEST_OPTS += ['-ffast-math']
- self.do_run(r'''
-#include <stdio.h>
-#include <stdlib.h>
-
-int main(int argc, char** argv) {
- char* endptr;
- --argc, ++argv;
- double total = 0.0;
- for (; argc; argc--, argv++) {
- total += strtod(*argv, &endptr);
- }
- printf("total: %g\n", total);
- return 0;
-}
-''', 'total: 19', ['5', '6', '8'])
-
- def test_zerodiv(self):
- self.do_run(r'''
- #include <stdio.h>
- int main(int argc, const char* argv[])
- {
- float f1 = 1.0f;
- float f2 = 0.0f;
- float f_zero = 0.0f;
+ test_path = path_from_root('tests', 'core', 'test_fast_math')
+ src, output = (test_path + s for s in ('.in', '.out'))
- float f3 = 0.0f / f2;
- float f4 = f2 / 0.0f;
- float f5 = f2 / f2;
- float f6 = f2 / f_zero;
+ self.do_run_from_file(src, output, ['5', '6', '8'])
- printf("f3: %f\n", f3);
- printf("f4: %f\n", f4);
- printf("f5: %f\n", f5);
- printf("f6: %f\n", f6);
+ def test_zerodiv(self):
+ test_path = path_from_root('tests', 'core', 'test_zerodiv')
+ src, output = (test_path + s for s in ('.in', '.out'))
- return 0;
- }
- ''', '''f3: nan
-f4: nan
-f5: nan
-f6: nan
-''')
+ self.do_run_from_file(src, output)
def test_zero_multiplication(self):
- src = '''
- #include <stdio.h>
- int main(int argc, char * argv[]) {
- int one = argc;
-
- printf("%d ", 0 * one);
- printf("%d ", 0 * -one);
- printf("%d ", -one * 0);
- printf("%g ", 0.0 * one);
- printf("%g ", 0.0 * -one);
- printf("%g", -one * 0.0);
- return 0;
- }
- '''
- self.do_run(src, '0 0 0 0 -0 -0')
+ test_path = path_from_root('tests', 'core', 'test_zero_multiplication')
+ src, output = (test_path + s for s in ('.in', '.out'))
+
+ self.do_run_from_file(src, output)
def test_isnan(self):
- src = r'''
- #include <stdio.h>
+ test_path = path_from_root('tests', 'core', 'test_isnan')
+ src, output = (test_path + s for s in ('.in', '.out'))
- int IsNaN(double x){
- int rc; /* The value return */
- volatile double y = x;
- volatile double z = y;
- rc = (y!=z);
- return rc;
- }
-
- int main() {
- double tests[] = { 1.0, 3.333, 1.0/0.0, 0.0/0.0, -1.0/0.0, -0, 0, -123123123, 12.0E200 };
- for (int i = 0; i < sizeof(tests)/sizeof(double); i++)
- printf("%d - %f - %d\n", i, tests[i], IsNaN(tests[i]));
- }
- '''
- self.do_run(src, '''0 - 1.000000 - 0
-1 - 3.333000 - 0
-2 - inf - 0
-3 - nan - 1
-4 - -inf - 0
-5 - 0.000000 - 0
-6 - 0.000000 - 0
-7 - -123123123.000000 - 0
-8 - 1.2e+201 - 0
-''')
+ self.do_run_from_file(src, output)
def test_globaldoubles(self):
- src = r'''
- #include <stdlib.h>
- #include <stdio.h>
-
- double testVu, testVv, testWu, testWv;
+ test_path = path_from_root('tests', 'core', 'test_globaldoubles')
+ src, output = (test_path + s for s in ('.in', '.out'))
- void Test(double _testVu, double _testVv, double _testWu, double _testWv)
- {
- testVu = _testVu;
- testVv = _testVv;
- testWu = _testWu;
- testWv = _testWv;
- printf("BUG?\n");
- printf("Display: Vu=%f Vv=%f Wu=%f Wv=%f\n", testVu, testVv, testWu, testWv);
- }
-
- int main(void)
- {
- double v1 = 465.1;
- double v2 = 465.2;
- double v3 = 160.3;
- double v4 = 111.4;
- Test(v1, v2, v3, v4);
- return 0;
- }
- '''
- self.do_run(src, 'BUG?\nDisplay: Vu=465.100000 Vv=465.200000 Wu=160.300000 Wv=111.400000')
+ self.do_run_from_file(src, output)
def test_math(self):
if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2')
- src = '''
- #include <stdio.h>
- #include <stdlib.h>
- #include <cmath>
- int main(int argc, char **argv)
- {
- printf("*%.2f,%.2f,%d", M_PI, -M_PI, (1/0.0) > 1e300); // could end up as infinity, or just a very very big number
- printf(",%d", isfinite(NAN) != 0);
- printf(",%d", isfinite(INFINITY) != 0);
- printf(",%d", isfinite(-INFINITY) != 0);
- printf(",%d", isfinite(12.3) != 0);
- printf(",%d", isinf(NAN) != 0);
- printf(",%d", isinf(INFINITY) != 0);
- printf(",%d", isinf(-INFINITY) != 0);
- printf(",%d", isinf(12.3) != 0);
- div_t div_result = div(23, 10);
- printf(",%d", div_result.quot);
- printf(",%d", div_result.rem);
- double sine = -1.0, cosine = -1.0;
- sincos(0.0, &sine, &cosine);
- printf(",%1.1lf", sine);
- printf(",%1.1lf", cosine);
- float fsine = -1.0f, fcosine = -1.0f;
- sincosf(0.0, &fsine, &fcosine);
- printf(",%1.1f", fsine);
- printf(",%1.1f", fcosine);
- fsine = sinf(1.1 + argc - 1);
- fcosine = cosf(1.1 + argc - 1);
- printf(",%1.1f", fsine);
- printf(",%1.1f", fcosine);
-