
import cdb.core.manip as manip

def test01():
    __cdbkernel__=create_scope()
    ex:= A_{m n} B^{m n} + q = C_{m} D^{m};
    manip.isolate(ex, $q$)
    exrhs=rhs(ex)
    tst:= C_{m} D^{m} - A_{m n} B^{m n} - @(exrhs);
    assert(tst==0)
    print("Test 01 passed")

test01()

def test02():
    __cdbkernel__=create_scope()
    ex := a - b = c + d;
    manip.to_rhs(ex, $b$)
    manip.to_rhs(ex, $a$)
    tst:= ( 0 = c + d + b - a ) - @(ex);
    assert(tst==$0=0$)
    print("Test 02 passed")
#to_lhs(ex0, $d + b$);

test02()

def test03():
    __cdbkernel__=create_scope()
    ex1 := a_{\mu}a^{\mu} + b = c;
    manip.eq_to_subrule(ex1)
    manip.get_lhs(ex1)
    manip.get_rhs(ex1)
    manip.multiply_through(ex1, $k_{\alpha}$)
    manip.swap_sides(ex1)
    tst:= (k_{\alpha} c = k_{\alpha} (a_{\mu} a^{\mu} + b ) ) - @(ex1);
    assert(tst==$0=0$)
    print("Test 03 passed")

test03()

def test04():
#    __cdbkernel__=create_scope()
    {f, i}::Symbol;
    ex := w_{f} - w_{i} > 2 \pi W_{a b}\beta^{a}\beta^{b};
    manip.get_lhs(ex)
    manip.get_rhs(ex)
    manip.swap_sides(ex)
    manip.swap_sides(ex)
    manip.multiply_through(ex, $2n$)
    exrhs=manip.get_rhs(ex)
    tst:= 4n \pi W_{a b} \beta^{a} \beta^{b}  - @(exrhs);
    assert(tst==0)
    print("Test 04 passed")

test04()

def test05():
    __cdbkernel__=create_scope()
    ex:= a, b, c;
    try:
        manip.eq_to_subrule(ex)
        assert(1==0)
    except Exception as e:
        print("Test 05a passed")
    try:
        manip.get_lhs(ex)
        assert(1==0)
    except Exception as e:
        print("Test 05b passed")
    try:
        manip.get_rhs(ex)
        assert(1==0)
    except Exception as e:
        print("Test 05c passed")
    try:
        manip.swap_sides(ex)
        assert(1==0)
    except Exception as e:
        print("Test 05d passed")
    try:
        manip.to_rhs(ex, $a$)
        assert(1==0)
    except Exception as e:
        print("Test 05e passed")
    try:
        manip.to_rhs(ex, $a$)
        assert(1==0)
    except Exception as e:
        print("Test 05f passed")
    try:
        t::Coordinate.
        {i, j}::Indices(values={t}, position=fixed).
        ex := a_{i};
        evaluate(ex, ${a_{t} = 1}$)
        manip.get_component(ex, $x$)
        manip.to_rhs(ex, $a$)
        assert(1==0)
    except Exception as e:
        print("Test 05g passed")

test05()

def test06():
    __cdbkernel__=create_scope()
    ex:= 2a+b+j*b+k*b = c+d;
    manip.to_lhs(ex)
    tst := (2a+b+j*b+k*b-c-d = 0) - @(ex);
    assert(tst==$0=0$)
    print("Test 06 passed")

test06()

def test07():
    __cdbkernel__=create_scope()
    ex:= p V = k T;
    ex2:= @(ex)/V;
    collect_factors(ex2)
    tst:= (p = k T / V) - @(ex2);
    assert(tst==$0=0$)
    print("Test 07a passed")
    
    try:
       ex1:= A = B;
       ex2:= C = D;
       ex3:= @(ex1) @(ex2);
       assert(1==0)
    except RuntimeError as e:
        print("Test 07b passed")
    
    ex3:= Q @(ex) R;
    assert( ex3 == $Q p V R = Q k T R$ )
    print("Test 07c passed")

test07()

