示例#1
0
    def test_numpy_floated(self):
        # TODO: add '10.2' instead of '10'. Somehow, adding 10.2 does not work
        source_file = io.StringIO("""
            module main;
            function void mlt(double* a, double *b, int size, int stride) {
                var int i = 0;
                var int ptr = 0;
                for ( i=0; i<size; i+=1 ) {
                  *(a + ptr) = *(a + ptr) + *(b + ptr) * 2 + 10;
                  ptr += stride;
                }
            }
            """)
        html_filename = make_filename(self.id()) + '.html'
        with open(html_filename, 'w') as f, HtmlReportGenerator(f) as r:
            m = load_code_as_module(source_file, reporter=r)

        import numpy as np
        a = np.array([12, 7, 3, 5, 42, 100], dtype=float)
        b = np.array([82, 2, 5, 8, 13, 600], dtype=float)
        c = np.array([186, 21, 23, 31, 78, 1310], dtype=float)

        ap = ctypes.cast(a.ctypes.data, ctypes.POINTER(ctypes.c_double))
        bp = ctypes.cast(b.ctypes.data, ctypes.POINTER(ctypes.c_double))

        m.mlt(ap, bp, len(a), a.itemsize)
        # print(a)
        # print(c)
        self.assertTrue(np.allclose(c, a))
示例#2
0
    def test_numpy(self):
        source_file = io.StringIO("""
            module main;
            function int find_first(int *a, int b, int size, int stride) {
                var int i = 0;
                var int ptr = 0;
                for ( i=0; i<size; i+=1 ) {
                  if ( *(a + ptr) == b ) {
                    return i;
                  }
                  ptr += stride;
                }

                return 0xff;
            }
            """)
        html_filename = make_filename(self.id()) + '.html'
        with open(html_filename, 'w') as f:
            with HtmlReportGenerator(f) as reporter:
                m = load_code_as_module(source_file, reporter=reporter)

        import numpy as np
        a = np.array([12, 7, 3, 5, 42, 8, 3, 5, 8, 1, 4, 6, 2], dtype=int)
        addr = ctypes.cast(a.ctypes.data, ctypes.POINTER(ctypes.c_int))

        # Cross fingers
        pos = m.find_first(addr, 42, len(a), a.itemsize)
        self.assertEqual(4, pos)

        pos = m.find_first(addr, 200, len(a), a.itemsize)
        self.assertEqual(0xff, pos)
示例#3
0
 def test_floated(self):
     """ Test floating point function """
     source_file = io.StringIO("""
         module main;
         function double add(double a, int b) {
             return a + b;
         }
         """)
     m = load_code_as_module(source_file)
     x = m.add(3.14, 101)
     # print(x, type(x))
     self.assertEqual(104.14, x)
示例#4
0
    def test_add(self):
        source_file = io.StringIO("""
              module main;
              function int add(int a, int b) {
                return a + b;
              }

              function int sub(int a, int b) {
                return a - b;
              }
            """)
        m = load_code_as_module(source_file)

        # Cross fingers
        self.assertEqual(3, m.add(1, 2))
        self.assertEqual(8, m.sub(10, 2))
示例#5
0
  var int x, y, z, i;
  x = a * b + 200;
  y = x / 20 + b;
  i = 0
  z = 0;
  for (i=0; i<10; i = i+1)
  {
    z = y * 4 + x * 2 + a * 3 + b * 8;
    y = y + z / 100 + i;
  }
  return x + y + z;
}

"""

mod = codepage.load_code_as_module(io.StringIO(src))

for a in range(10):
    x = 7 * a
    y = 13 * a
    assert heavy_math(x, y) == mod.math(x, y), str(x)


def mk_func(f):
    def x():
        for a in range(100):
            f(a, 13)

    return x