<commit_before>import claripy
import ana
import nose
import pickle
import tempfile

import logging
l = logging.getLogger('claripy.test.serial')

def test_pickle_ast():
    bz = claripy.backends.z3

    a = claripy.BVV(1, 32)
    b = claripy.BVS('x', 32, explicit_name=True)

    c = a+b
    nose.tools.assert_equal(bz.convert(c).__module__, 'z3')
    nose.tools.assert_equal(str(bz.convert(c)), '1 + x')

    c_copy = pickle.loads(pickle.dumps(c, -1))
    nose.tools.assert_equal(bz.convert(c_copy).__module__, 'z3')
    nose.tools.assert_equal(str(bz.convert(c_copy)), '1 + x')

def test_pickle_frontend():
    s = claripy.FullFrontend(claripy.backends.z3)
    x = claripy.BVS('x', 32)

    s.add(x == 1)
    assert s.eval(x, 10), (1,)

    ss = pickle.dumps(s)
    del s
    import gc
    gc.collect()

    s = pickle.loads(ss)
    assert s.eval(x, 10), (1,)

def test_datalayer():
    l.info("Running test_datalayer")

    pickle_dir = tempfile.mkdtemp()
    ana.set_dl(pickle_dir=pickle_dir)
    l.debug("Pickling to %s",pickle_dir)

    a = claripy.BVV(1, 32)
    b = claripy.BVS("x", 32)
    c = a + b
    d = a+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b

    l.debug("Storing!")
    a.ana_store()
    c_info = c.ana_store()
    d_info = d.ana_store()

    l.debug("Loading!")
    ana.set_dl(pickle_dir=pickle_dir)
    #nose.tools.assert_equal(len(claripy.dl._cache), 0)

    cc = claripy.ast.BV.ana_load(c_info)
    nose.tools.assert_equal(str(cc), str(c))
    cd = claripy.ast.BV.ana_load(d_info)
    nose.tools.assert_equal(str(cd), str(d))

    l.debug("Time to test some solvers!")
    s = claripy.FullFrontend(claripy.backends.z3)
    x = claripy.BVS("x", 32)
    s.add(x == 3)
    s.finalize()
    ss = claripy.FullFrontend.ana_load(s.ana_store())
    nose.tools.assert_equal(str(s.constraints), str(ss.constraints))
    nose.tools.assert_equal(str(s.variables), str(ss.variables))

    s = claripy.CompositeFrontend(claripy.FullFrontend(claripy.backends.z3))
    x = claripy.BVS("x", 32)
    s.add(x == 3)
    s.finalize()
    ss = claripy.CompositeFrontend.ana_load(s.ana_store())
    old_constraint_sets = [[hash(j) for j in k.constraints] for k in s._solver_list]
    new_constraint_sets = [[hash(j) for j in k.constraints] for k in ss._solver_list]<commit_msg>update serialization tests for ana changes<commit_after>import claripy
import ana
import nose
import pickle
import tempfile

import logging
l = logging.getLogger('claripy.test.serial')

def test_pickle_ast():
    bz = claripy.backends.z3

    a = claripy.BVV(1, 32)
    b = claripy.BVS('x', 32, explicit_name=True)

    c = a+b
    nose.tools.assert_equal(bz.convert(c).__module__, 'z3')
    nose.tools.assert_equal(str(bz.convert(c)), '1 + x')

    c_copy = pickle.loads(pickle.dumps(c, -1))
    nose.tools.assert_equal(bz.convert(c_copy).__module__, 'z3')
    nose.tools.assert_equal(str(bz.convert(c_copy)), '1 + x')

def test_pickle_frontend():
    s = claripy.FullFrontend(claripy.backends.z3)
    x = claripy.BVS('x', 32)

    s.add(x == 1)
    assert s.eval(x, 10), (1,)

    ss = pickle.dumps(s)
    del s
    import gc
    gc.collect()

    s = pickle.loads(ss)
    assert s.eval(x, 10), (1,)

def test_datalayer():
    l.info("Running test_datalayer")

    pickle_dir = tempfile.mkdtemp()
    ana.set_dl(ana.DirDataLayer(pickle_dir))
    l.debug("Pickling to %s",pickle_dir)

    a = claripy.BVV(1, 32)
    b = claripy.BVS("x", 32)
    c = a + b
    d = a+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b+b

    l.debug("Storing!")
    a.ana_store()
    c_info = c.ana_store()
    d_info = d.ana_store()

    l.debug("Loading!")
    ana.set_dl(ana.DirDataLayer(pickle_dir))
    #nose.tools.assert_equal(len(claripy.dl._cache), 0)

    cc = claripy.ast.BV.ana_load(c_info)
    nose.tools.assert_equal(str(cc), str(c))
    cd = claripy.ast.BV.ana_load(d_info)
    nose.tools.assert_equal(str(cd), str(d))

    l.debug("Time to test some solvers!")
    s = claripy.FullFrontend(claripy.backends.z3)
    x = claripy.BVS("x", 32)
    s.add(x == 3)
    s.finalize()
    ss = claripy.FullFrontend.ana_load(s.ana_store())
    nose.tools.assert_equal(str(s.constraints), str(ss.constraints))
    nose.tools.assert_equal(str(s.variables), str(ss.variables))

    s = claripy.CompositeFrontend(claripy.FullFrontend(claripy.backends.z3))
    x = claripy.BVS("x", 32)
    s.add(x == 3)
    s.finalize()
    ss = claripy.CompositeFrontend.ana_load(s.ana_store())
    old_constraint_sets = [[hash(j) for j in k.constraints] for k in s._solver_list]
    new_constraint_sets = [[hash(j) for j in k.constraints] for k in ss._solver_list]
{'fraction_non_alphanumeric': 0.1711501800466003, 'fraction_numerical': 0.014827367083245075, 'mean_word_length': 3.620352250489237, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1317107', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2069, 'n_words': 312}