
def test01():
    __cdbkernel__=create_scope()
    ex:= A + B D G + C D A;
    zoom(_, $D Q??$)
    substitute(_, $A = Q$)
    unzoom(_)
    tst:= A + B D G + C D Q - @(ex);
    assert(tst==0)
    print("Test 01a passed")
    ex:= A + B D G + C D A;
    zoom(_, $D Q??$)
    substitute(_, $D = 0$)
    unzoom(_)
    tst:= A - @(ex);
    assert(tst==0)
    print("Test 01b passed")
    ex:= R = \int{A + B D G + C D A}{x};
    zoom(_, $D Q??$)
    substitute(_, $A = Q$)
    unzoom(_)
    tst:= ( R = \int{A + B D G + C D Q}{x} ) - @(ex);
    print("Test 01c passed")
    ex:= R = \int{A + B D G + C D A}{x};
    zoom(_, $D Q??$)
    substitute(_, $A = 0$)
    unzoom(_)
    tst:= ( R = \int{A + B D G}{x} ) - @(ex);
    assert(tst== $0 = 0$)
    print("Test 01d passed")
    ex:= R + S = \int{A + B D G + C D A}{x};
    zoom(_, $D Q??$)
    substitute(_, $A = 0$)
    unzoom(_)
    tst:= ( R + S = \int{A + B D G}{x} ) - @(ex);
    assert(tst== $0 = 0$)
    print("Test 01e passed")

test01()

# def test02():
#     __cdbkernel__=create_scope()
# a:= A,B,C;
# b:= D,E,F;
# c:= Q;
# d:= R+S;
# ex1=a+b
# ex2=a+c
# ex3=c+a
# ex4=a+d
# tst1:= {A,B,C,D,E,F};
# tst2:= {A,B,C,Q};
# tst3:= {Q,A,B,C};
# tst4:= {A,B,C,R+S};
# assert(tst1==ex1)
# assert(tst2==ex2)
# assert(tst3==ex3)
# assert(tst4==ex4)
#     print("Test 02 passed")
# 
# test02()

def test03():
    # zoom should also work to select terms in an integral
    # and then replace them later.
    __cdbkernel__=create_scope()
    ex:= 5\int{A+3 B M + C N+D}{dx};
    zoom(_, $B Q??$)
    substitute(_, $B->2 P$)
    unzoom(_)
    tst:= 5\int{A + 6 P M + C N + D}{dx} - @(ex);
    assert(tst==0)
    print("Test 03 passed")

test03()

def test04():
    # zoom should also work to select terms in an integral
    # and then replace them later.
    __cdbkernel__=create_scope()
    ex:= 5\int{A+3 B M + C N+D}{dx};
    zoom(_, $B Q??$)
    substitute(_, $B->0$)
    unzoom(_)
    tst:= 5\int{A + C N + D}{dx} - @(ex);
    assert(tst==0)
    print("Test 04 passed")

test04()

def test05():
    __cdbkernel__=create_scope()
    \partial{#}::PartialDerivative;
    ex:=\int{A_{i} h_{j} + \partial_{i}{B_{j}} D }{x};
    zoom(_, $\partial_{i}{B_{j}} Q??$)
    substitute(_, $D -> E$)
    unzoom(_)
    tst:=\int{A_{i} h_{j} + \partial_{i}{B_{j}} E}{x} - @(ex);
    assert(tst==0)
    print("Test 05 passed")

test05()


# ex:= \sin( 3 \int{ A E + B_{m} C^{m} + Q D }{x} ) + \cos( \int{ A K + B E }{x} );
# zoom(_, $A Q??$);
# substitute(_, $E->1$);
# tst:= \sin( 3 \int{ A + B_{m} C^{m} + Q D }{x} ) + \cos( \int{ A K + B E }{x} ) - @(ex);

def test06():
    __cdbkernel__=create_scope()
    \partial{#}::PartialDerivative;
    ex:=  \int{ 3 A_{i j} + 2 \partial_{i}{\partial_{j}{h}}}{x};
    zoom(_, $A_{i j}$)
    substitute(_,$A_{i j} -> B_{i j}$)
    unzoom(_)
    tst:= \int{ 3 B_{i j} + 2 \partial_{i}{\partial_{j}{h}}}{x} - @(ex);
    assert(tst==0)
    print("Test 06 passed")

test06()

def test07():
    __cdbkernel__=create_scope()
    \partial{#}::PartialDerivative;
    ex:=  \int{ 3 h A_{i j} + 2 \partial_{i}{\partial_{j}{h}}}{x};
    zoom(_, $A_{i j} Q??$)
    substitute(_,$A_{i j} -> B_{i j}$)
    integrate_by_parts(_, $h$)  
    unzoom(_)
    tst:= \int{ 3 h B_{i j} + 2 \partial_{i}{\partial_{j}{h}}}{x} - @(ex);
    assert(tst==0)
    print("Test 07 passed")

test07()

def test08():
    __cdbkernel__=create_scope()
    ex:= A + B D + E B + C Q + D;
    cp1:= @(ex);        
    zoom(ex, $B Q??$)
    cp2:= @(ex);        
    zoom(ex, $E Q??$)
    unzoom(ex)
    tst:= @(cp2) - @(ex);
    assert(tst==0)
    print("Test 08a passed")
    unzoom(ex)
    tst:= @(cp1) - @(ex);
    assert(tst==0)
    print("Test 08b passed")
    ex:= A + B D + E B + C Q + D;
    cp1:= @(ex)        
    zoom(ex, $B Q??$)
    cp2:= @(ex)        
    zoom(ex, $E Q??$)
    unzoom(ex, repeat=True)
    tst:= @(cp1) - @(ex);
    assert(tst==0)
    print("Test 08c passed")

test08()        
            
        
#def test08():

# 
# \epsilon::Weight(label=field, value=1);
# Exp:=\epsilon**4+\epsilon*(\epsilon**3+5);
# zoom(_, $\epsilon * A??$);
# distribute(_);
# unzoom(_);
# drop_weight(_, $field=4$);

