Created
August 11, 2022 00:23
-
-
Save yonatanzunger/b27f7404f508c7d50af3203db28bd052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dis | |
import unittest | |
from humu.storage.impl.fast_comparator import Comparison, makeFastComparator | |
class testFastComparator(unittest.TestCase): | |
def testSimpleComparator(self): | |
# This should make a function | |
# def compareTwo(x1, x2): | |
# return x2 == b'1234' and x1 > b'0000' | |
compareTwo = makeFastComparator( | |
2, | |
[ | |
Comparison(1, dis.cmp_op.index('=='), b'1234'), | |
Comparison(0, dis.cmp_op.index('>'), b'0000'), | |
], | |
) | |
self.assertTrue(compareTwo(b'5555', b'1234')) | |
# Fail the first test | |
self.assertFalse(compareTwo(b'5555', b'1000')) | |
# Fail the second test | |
self.assertFalse(compareTwo(b'****', b'1234')) | |
def testCheckNothing(self): | |
noTestsCompare = makeFastComparator(2, []) | |
self.assertTrue(noTestsCompare('asdfa', 'dfhgdf')) | |
def testCompareThree(self): | |
compareThree = makeFastComparator( | |
3, | |
[ | |
Comparison(0, dis.cmp_op.index('=='), b'1234'), | |
Comparison(1, dis.cmp_op.index('=='), b'2345'), | |
Comparison(2, dis.cmp_op.index('=='), b'3456'), | |
], | |
) | |
self.assertTrue(compareThree(b'1234', b'2345', b'3456')) | |
self.assertFalse(compareThree(b'0000', b'2345', b'3456')) | |
self.assertFalse(compareThree(b'1234', b'0000', b'3456')) | |
self.assertFalse(compareThree(b'1234', b'2345', b'0000')) | |
def testCompareList(self): | |
compareThree = makeFastComparator( | |
3, | |
[ | |
Comparison(0, dis.cmp_op.index('in'), [b'1234', b'2345', b'3456']), | |
Comparison(1, dis.cmp_op.index('in'), [b'1234']), | |
Comparison(2, dis.cmp_op.index('in'), [b'2345', b'3456']), | |
], | |
) | |
self.assertTrue(compareThree(b'1234', b'1234', b'2345')) | |
self.assertTrue(compareThree(b'2345', b'1234', b'3456')) | |
self.assertFalse(compareThree(b'0000', b'1234', b'3456')) | |
self.assertFalse(compareThree(b'1234', b'1234', b'1234')) | |
self.assertFalse(compareThree(b'1234', b'1234', b'0000')) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment