예제 #1
0
    def recv(self, atom, args):
        if atom is RUN_1:
            return IntObject(int(unwrapStr(args[0]).encode('utf-8')))

        if atom is RUN_2:
            inp = unwrapStr(args[0])
            radix = unwrapInt(args[1])
            try:
                v = int(inp.encode("utf-8"), radix)
            except ValueError:
                raise userError(u"Invalid literal for base %d: %s" %
                                (radix, inp))
            return IntObject(v)

        if atom is FROMBYTES_1:
            return IntObject(int(unwrapBytes(args[0])))

        if atom is FROMBYTES_2:
            bs = unwrapBytes(args[0])
            radix = unwrapInt(args[1])
            try:
                v = int(bs, radix)
            except ValueError:
                raise userError(u"Invalid literal for base %d: %s" %
                                (radix, bytesToString(bs)))
            return IntObject(v)

        raise Refused(self, atom, args)
예제 #2
0
 def testListEqualityRecursion(self):
     # Yes, this is very hacky.
     first = ConstList([IntObject(42)])
     first.strategy.append(first, [first])
     second = ConstList([IntObject(42)])
     second.strategy.append(second, [second])
     self.assertEqual(optSame(first, second), EQUAL)
예제 #3
0
 def testRefEqualitySettled(self):
     with scopedVat(testingVat()):
         first, r = makePromise()
         r.resolve(IntObject(42))
         second, r = makePromise()
         r.resolve(IntObject(42))
         self.assertEqual(optSame(first, second), EQUAL)
예제 #4
0
    def recv(self, atom, args):
        if atom is EXITSTATUS_0:
            return IntObject(self.exitStatus)

        if atom is TERMINATIONSIGNAL_0:
            return IntObject(self.terminationSignal)

        raise Refused(self, atom, args)
예제 #5
0
 def testSurprisingMapCorruption(self):
     d = monteMap()
     d[IntObject(1)] = IntObject(2)
     m = ConstMap(d)
     f = m.call(u"diverge", [])
     f.call(u"removeKey", [IntObject(1)])
     result = m.call(u"get", [IntObject(1)])
     self.assertEqual(result.getInt(), 2)
예제 #6
0
파일: test_data.py 프로젝트: washort/typhon
    def testMulDouble(self):
        """
        Ints are promoted by doubles during multiplication.
        """

        i = IntObject(4)
        result = i.call(u"multiply", [DoubleObject(2.1)])
        self.assertTrue(isinstance(result, DoubleObject))
        self.assertEqual(result.getDouble(), 8.4)
예제 #7
0
    def testMulDouble(self):
        """
        Ints are promoted by doubles during multiplication.
        """

        i = IntObject(4)
        result = i.call(u"multiply", [DoubleObject(2.1)])
        self.assertTrue(isinstance(result, DoubleObject))
        self.assertEqual(result.getDouble(), 8.4)
예제 #8
0
파일: timeit.py 프로젝트: washort/typhon
def bench(obj, name):
    name = unwrapStr(name).encode("utf-8")

    if not benchmarkSettings.enabled:
        debug_print("Not running benchmark", name,
                    "since benchmarking is disabled")
        return ConstList([IntObject(0), DoubleObject(0.0)])

    debug_print("Benchmarking", name)

    # Step 1: Calibrate timing loop.
    debug_print("Calibrating timing loop...")
    # Unroll do-while iteration.
    loops = 1
    debug_print("Trying 1 loop...")
    taken = time.time()
    obj.call(u"run", [])
    taken = time.time() - taken
    while taken < 1.0 and loops < 100000000:
        debug_print("Took", taken, "seconds to run", loops, "loops")
        loops *= 10
        debug_print("Trying", loops, "loops...")
        acc = 0
        taken = time.time()
        while acc < loops:
            acc += 1
            obj.call(u"run", [])
        taken = time.time() - taken
    debug_print("Okay! Will take", loops, "loops at", taken, "seconds")

    # Step 2: Take trials.
    debug_print("Taking trials...")
    trialCount = 3 - 1
    # Unroll first iteration to get maximum.
    acc = 0
    taken = time.time()
    while acc < loops:
        acc += 1
        obj.call(u"run", [])
    taken = time.time() - taken
    result = taken
    while trialCount:
        trialCount -= 1
        acc = 0
        taken = time.time()
        while acc < loops:
            acc += 1
            obj.call(u"run", [])
        taken = time.time() - taken
        if taken < result:
            result = taken

    # All done!
    return ConstList([IntObject(loops), DoubleObject(taken)])
예제 #9
0
파일: crypt.py 프로젝트: washort/typhon
    def recv(self, atom, args):
        if atom is GETALGORITHM_0:
            return StrObject(u"CSPRNG (libsodium)")

        if atom is GETENTROPY_0:
            # uint32_t in the FFI, so exactly 32 bits every time.
            return ConstList([
                IntObject(32),
                IntObject(intmask(rsodium.randombytesRandom()))
            ])

        raise Refused(self, atom, args)
예제 #10
0
    def recv(self, atom, args):

        # and/1
        if atom is AND_1:
            return wrapBool(self._b and unwrapBool(args[0]))

        # butNot/1
        if atom is BUTNOT_1:
            return wrapBool(self._b and not unwrapBool(args[0]))

        # not/0
        if atom is NOT_0:
            return wrapBool(not self._b)

        # op__cmp/1
        if atom is OP__CMP_1:
            from typhon.objects.data import IntObject
            return IntObject(self._b - unwrapBool(args[0]))

        # or/1
        if atom is OR_1:
            return wrapBool(self._b or unwrapBool(args[0]))

        # pick/2
        if atom is PICK_2:
            return args[0] if self._b else args[1]

        # xor/1
        if atom is XOR_1:
            return wrapBool(self._b ^ unwrapBool(args[0]))

        raise Refused(self, atom, args)
예제 #11
0
 def testHashEqual(self):
     d = monteSet()
     d[IntObject(42)] = None
     d[CharObject(u'¡')] = None
     a = ConstSet(d)
     b = ConstSet(d)
     self.assertEqual(a.hash(), b.hash())
예제 #12
0
    def testSwitchableBecomesResolved(self):
        with scopedVat(testingVat()):
            p, r = makePromise()
            self.assertFalse(isResolved(p))

            r.resolve(IntObject(42))
            self.assertTrue(isResolved(p))
예제 #13
0
파일: lists.py 프로젝트: zarutian/typhon
 def next(self, ej):
     if self._index < self.size:
         rv = [IntObject(self._index), self.objects[self._index]]
         self._index += 1
         return rv
     else:
         throwStr(ej, u"Iterator exhausted")
예제 #14
0
파일: runtime.py 프로젝트: washort/typhon
    def recv(self, atom, args):
        if atom is GETBUCKETS_0:
            d = monteMap()
            for name, count in self.buckets.items():
                size = self.sizes.get(name, -1)
                d[StrObject(name)] = ConstList(
                    [IntObject(size), IntObject(count)])
            return ConstMap(d)

        if atom is GETMEMORYUSAGE_0:
            return IntObject(self.memoryUsage)

        if atom is GETOBJECTCOUNT_0:
            return IntObject(self.objectCount)

        raise Refused(self, atom, args)
예제 #15
0
    def testSwitchableChains(self):
        with scopedVat(testingVat()):
            p, r = makePromise()
            p2, r2 = makePromise()

            r.resolve(p2)
            self.assertFalse(isResolved(p))

            r2.resolve(IntObject(42))
            self.assertTrue(isResolved(p))
예제 #16
0
    def recv(self, atom, args):
        if atom is NEXT_1:
            if self._index < self.size:
                rv = [IntObject(self._index), self.objects[self._index]]
                self._index += 1
                return ConstList(rv)
            else:
                ej = args[0]
                ej.call(u"run", [StrObject(u"Iterator exhausted")])

        raise Refused(self, atom, args)
예제 #17
0
    def recv(self, atom, args):
        if atom is GETARITY_0:
            return IntObject(self.arity)

        if atom is GETDOCSTRING_0:
            if self.docstring is not None:
                return StrObject(self.docstring)
            return NullObject

        if atom is GETVERB_0:
            return StrObject(self.verb)

        raise Refused(self, atom, args)
예제 #18
0
    def op__cmp(self, other):
        """
        Perform a subset comparison.
        """

        if len(self.objectSet) < len(other):
            smaller = self.objectSet
            larger = other
        else:
            smaller = other
            larger = self.objectSet

        for item in smaller.keys():
            if item not in larger:
                return Incomparable

        # smaller is a subset of larger.
        if len(self.objectSet) == len(other):
            return IntObject(0)
        elif len(self.objectSet) < len(other):
            return IntObject(-1)
        else:
            return IntObject(1)
예제 #19
0
    def recv(self, atom, args):
        # _makeIterator/0: Create an iterator for this collection's contents.
        if atom is _MAKEITERATOR_0:
            return self._makeIterator()

        if atom is _PRINTON_1:
            printer = args[0]
            self.printOn(printer)
            return NullObject

        # contains/1: Determine whether an element is in this collection.
        if atom is CONTAINS_1:
            return wrapBool(self.contains(args[0]))

        # size/0: Get the number of elements in the collection.
        if atom is SIZE_0:
            return IntObject(self.size())

        # slice/1 and slice/2: Select a subrange of this collection.
        if atom is SLICE_1:
            start = unwrapInt(args[0])
            try:
                return self.slice(start)
            except IndexError:
                raise userError(u"slice/1: Index out of bounds")

        # slice/1 and slice/2: Select a subrange of this collection.
        if atom is SLICE_2:
            start = unwrapInt(args[0])
            stop = unwrapInt(args[1])
            try:
                return self.slice(start, stop)
            except IndexError:
                raise userError(u"slice/1: Index out of bounds")

        # snapshot/0: Create a new constant collection with a copy of the
        # current collection's contents.
        if atom is SNAPSHOT_0:
            return self.snapshot()

        if atom is JOIN_1:
            l = unwrapList(args[0])
            return self.join(l)

        return self._recv(atom, args)
예제 #20
0
    def recv(self, atom, args):
        if atom is GETARGUMENTS_0:
            return ConstList(
                [StrObject(arg.decode("utf-8")) for arg in self.config.argv])

        if atom is GETENVIRONMENT_0:
            # XXX monteMap()
            d = monteMap()
            for key, value in os.environ.items():
                k = StrObject(key.decode("utf-8"))
                v = StrObject(value.decode("utf-8"))
                d[k] = v
            return ConstMap(d)

        if atom is GETPID_0:
            return IntObject(os.getpid())

        if atom is INTERRUPT_0:
            os.kill(os.getpid(), signal.SIGINT)
            return NullObject

        raise Refused(self, atom, args)
예제 #21
0
    def recv(self, atom, args):
        if atom is GETARGUMENTS_0:
            return ConstList([BytesObject(arg) for arg in self.argv])

        if atom is GETENVIRONMENT_0:
            # XXX monteMap()
            d = monteMap()
            for key, value in self.env.items():
                k = BytesObject(key)
                v = BytesObject(value)
                d[k] = v
            return ConstMap(d)

        if atom is GETPID_0:
            return IntObject(self.pid)

        if atom is INTERRUPT_0:
            os.kill(self.pid, signal.SIGINT)
            return NullObject

        if atom is WAIT_0:
            return self.makeWaiter()

        raise Refused(self, atom, args)
예제 #22
0
 def testAdd(self):
     i = IntObject(32)
     result = i.call(u"add", [IntObject(11)])
     self.assertEqual(result.getInt(), 43)
예제 #23
0
파일: test_data.py 프로젝트: washort/typhon
 def testGetNegative(self):
     s = StrObject(u"index")
     self.assertRaises(UserException, s.call, u"get", [IntObject(-1)])
예제 #24
0
 def testComplement(self):
     i = IntObject(5)
     result = i.call(u"complement", [])
     self.assertEqual(result.getInt(), -6)
예제 #25
0
파일: test_data.py 프로젝트: washort/typhon
 def testXorInt(self):
     bi = BigInt(rbigint.fromint(0xcccc))
     result = bi.call(u"xor", [IntObject(0xaaaa)])
     self.assertTrue(result.bi.int_eq(0x6666))
예제 #26
0
파일: test_data.py 프로젝트: washort/typhon
 def testOpCmpInt(self):
     bi = BigInt(rbigint.fromint(6))
     i = IntObject(2)
     result = bi.call(u"op__cmp", [i])
     self.assertEqual(result.getInt(), 1)
예제 #27
0
 def testOpCmpDouble(self):
     i = IntObject(2)
     result = i.call(u"op__cmp", [DoubleObject(2.0)])
     self.assertEqual(result.getInt(), 0)
예제 #28
0
파일: test_data.py 프로젝트: washort/typhon
 def testBitLength(self):
     i = IntObject(42)
     result = i.call(u"bitLength", [])
     self.assertEqual(result.getInt(), 6)
예제 #29
0
파일: test_data.py 프로젝트: washort/typhon
 def testApproxDivideNaN(self):
     i = IntObject(0)
     result = i.call(u"approxDivide", [i])
     self.assertTrue(math.isnan(result.getDouble()))
예제 #30
0
 def testMin(self):
     i = IntObject(3)
     result = i.call(u"min", [IntObject(5)])
     self.assertEqual(result.getInt(), 3)
예제 #31
0
 def testAddDouble(self):
     i = IntObject(32)
     result = i.call(u"add", [DoubleObject(1.1)])
     self.assertAlmostEqual(result.getDouble(), 33.1)
예제 #32
0
 def testModPow(self):
     i = IntObject(3)
     result = i.call(u"modPow", [IntObject(1000000), IntObject(255)])
     self.assertEqual(result.getInt(), 171)
예제 #33
0
 def testPowSmall(self):
     i = IntObject(5)
     result = i.call(u"pow", [IntObject(7)])
     self.assertEqual(result.getInt(), 78125)
예제 #34
0
 def testOr(self):
     i = IntObject(0x3)
     result = i.call(u"or", [IntObject(0x5)])
     self.assertEqual(result.getInt(), 0x7)
예제 #35
0
 def testOpCmpBigInt(self):
     i = IntObject(2)
     bi = BigInt(rbigint.fromint(6))
     result = i.call(u"op__cmp", [bi])
     self.assertEqual(result.getInt(), -1)
예제 #36
0
 def testMax(self):
     i = IntObject(3)
     result = i.call(u"max", [IntObject(5)])
     self.assertEqual(result.getInt(), 5)
예제 #37
0
파일: test_data.py 프로젝트: washort/typhon
 def testSubtract(self):
     i = IntObject(5)
     result = i.call(u"subtract", [IntObject(15)])
     self.assertAlmostEqual(result.getInt(), -10)
예제 #38
0
 def testShiftLeftLarge(self):
     i = IntObject(0x5c5c)
     result = i.call(u"shiftLeft", [IntObject(64)])
     bi = rbigint.fromint(0x5c5c).lshift(64)
     self.assertTrue(result.bi.eq(bi))
예제 #39
0
 def testApproxDivide(self):
     i = IntObject(4)
     result = i.call(u"approxDivide", [IntObject(2)])
     self.assertAlmostEqual(result.getDouble(), 2.0)
예제 #40
0
 def testInt(self):
     i = IntObject(42)
     self.assertTrue(i.isSettled())
예제 #41
0
 def testShiftLeftFar(self):
     i = IntObject(0x1)
     result = i.call(u"shiftLeft", [IntObject(65)])
     bi = rbigint.fromint(0x1).lshift(65)
     self.assertTrue(result.bi.eq(bi))
예제 #42
0
파일: test_data.py 프로젝트: washort/typhon
 def testShiftRightLarge(self):
     i = IntObject(0x7fffffffffffffff)
     result = i.call(u"shiftRight", [IntObject(64)])
     self.assertEqual(result.getInt(), 0x0)
예제 #43
0
 def testShiftRight(self):
     i = IntObject(0xf0)
     result = i.call(u"shiftRight", [IntObject(5)])
     self.assertEqual(result.getInt(), 0x7)
예제 #44
0
파일: test_data.py 프로젝트: washort/typhon
 def testSubtractDouble(self):
     i = IntObject(5)
     result = i.call(u"subtract", [DoubleObject(1.5)])
     self.assertAlmostEqual(result.getDouble(), 3.5)
예제 #45
0
 def testShiftRightLarge(self):
     i = IntObject(0x7fffffffffffffff)
     result = i.call(u"shiftRight", [IntObject(64)])
     self.assertEqual(result.getInt(), 0x0)
예제 #46
0
파일: test_data.py 프로젝트: washort/typhon
 def testShiftRight(self):
     bi = BigInt(rbigint.fromint(42))
     result = bi.call(u"shiftRight", [IntObject(2)])
     self.assertTrue(result.bi.int_eq(10))
예제 #47
0
 def testSubtract(self):
     i = IntObject(5)
     result = i.call(u"subtract", [IntObject(15)])
     self.assertAlmostEqual(result.getInt(), -10)
예제 #48
0
파일: test_data.py 프로젝트: washort/typhon
 def testAndInt(self):
     bi = BigInt(rbigint.fromint(0x3fffffffffffffff).int_mul(3))
     result = bi.call(u"and", [IntObject(0xffff)])
     self.assertTrue(result.bi.int_eq(0xfffd))
예제 #49
0
 def testSubtractDouble(self):
     i = IntObject(5)
     result = i.call(u"subtract", [DoubleObject(1.5)])
     self.assertAlmostEqual(result.getDouble(), 3.5)
예제 #50
0
파일: test_data.py 프로젝트: washort/typhon
 def testGet(self):
     s = StrObject(u"index")
     result = s.call(u"get", [IntObject(2)])
     self.assertEqual(result._c, u'd')
예제 #51
0
 def testBitLength(self):
     i = IntObject(42)
     result = i.call(u"bitLength", [])
     self.assertEqual(result.getInt(), 6)
예제 #52
0
 def testPow(self):
     i = IntObject(3)
     result = i.call(u"pow", [IntObject(100)])
     self.assertTrue(result.bi.eq(rbigint.fromint(3).pow(rbigint.fromint(100))))
예제 #53
0
 def testApproxDivideBigInt(self):
     i = IntObject(7937000378463977)
     # Hack.
     bi = BigInt(rbigint.fromint(1000000000000000000).int_mul(10))
     result = i.call(u"approxDivide", [bi])
     self.assertAlmostEqual(result._d, 0.0007937000378463977)