
def test01():
    __cdbkernel__=create_scope()
    x^{\mu}::Coordinate;
    x::Coordinate;
    #x::Implicit(x^{\mu});
    \phi::Depends(x);
    \partial{#}::PartialDerivative;
    ex:= \int{ \partial_{\mu}{ \epsilon^{\mu} \phi } }{x};
    integrate_by_parts(ex, $\epsilon^{\mu}$)
    assert(ex==0)
    print('Test 01 passed')

test01()

def test02():
    __cdbkernel__=create_scope()
    \partial{#}::PartialDerivative;
    ex:= \int{ \partial_{\mu}{ \delta{\phi} } \phi \chi }{x};
    integrate_by_parts(ex, $\epsilon$)
    integrate_by_parts(ex, $\delta{\phi}$, repeat=True)
    tst:= \int{ \delta{\phi} \partial_{\mu}{  \phi \chi } }{x} + @(ex);
    assert(tst==0)
    print('Test 02 passed')
    
test02()
    
def test03():
    __cdbkernel__=create_scope()
    \partial{#}::PartialDerivative;
    ex:= \int{ \phi \chi \partial_{\mu}{ \delta{\phi} } }{x};
    integrate_by_parts(ex, $\delta{\phi}$)
    tst:= \int{ \partial_{\mu}{\phi \chi}  \delta{\phi} }{x} + @(ex);
    assert(tst==0)
    print('Test 03 passed')
    
test03()
    
def test04():
    __cdbkernel__=create_scope()
    \partial{#}::PartialDerivative;
    ex:= \int{ \phi \chi \partial_{\mu}{ \delta{\phi} } + \partial_{\mu}{ \delta{\phi} } \phi \chi }{x};
    integrate_by_parts(ex, $\delta{\phi}$)
    tst:= \int{ -\partial_{\mu}{ \phi \chi } \delta{\phi} - \delta{\phi} \partial_{\mu}{ \phi \chi } }{x} - @(ex);
    assert(tst==0)
    print('Test 04 passed')
    
test04()

def test05():
    __cdbkernel__=create_scope()
    \partial{#}::PartialDerivative;
    ex:= \int{ \phi \chi \partial_{\mu}{ \delta{\phi} } \Phi \Chi }{x};
    integrate_by_parts(ex, $\delta{\phi}$)
    tst:=\int{ -\partial_{\mu}{ \phi \chi } \delta{\phi} \Phi \Chi - \phi \chi \delta{\phi} \partial_{\mu}{ \Phi \Chi} }{x} - @(ex);
    assert(tst==0)
    print('Test 05 passed')
    
test05()

def test06():
    __cdbkernel__=create_scope()
    x::Coordinate;
    \phi::Depends(x);
    \delta{\phi}::Depends(x);
    \partial{#}::PartialDerivative;
    ex:= -1/2 \int{m**2 \phi**2}{x};
    vary(_, $\phi -> \delta{\phi}$)
    tst:= - \int{m**2 \delta{\phi}\phi}{x} - @(ex);
    assert(tst)
    print('Test 06 passed')
    
test06()

def test07():
    __cdbkernel__=create_scope()
    ex:= \commutator{a}{b};
    vary(_, $a -> \delta{a}, b->\delta{b}$)
    tst:= \commutator{\delta{a}}{b} + \commutator{a}{\delta{b}} - @(ex);
    assert(tst==0)
    print("Test 07 passed")

test07()

# def test08():
#     __cdbkernel__=create_scope()
#     ex:= \log(a);
#     vary(_, $a -> \delta{a}$)
#     tst:= \log(\delta{a}) - @(ex);
#     assert(tst==0)
#
# test08()

def test08():
    __cdbkernel__=create_scope()
    \partial{#}::PartialDerivative;
    ex:= \int{ Q + R +  A \partial{B} C }{x};
    integrate_by_parts(_, $B$)
    tst:= \int{ Q + R - \partial{A} B C - A B \partial{C} }{x} - @(ex);
    assert(tst==0)
    print("Test 08 passed")

test08()
 
def test09():
    __cdbkernel__=create_scope()
    \partial{#}::PartialDerivative;
    \delta{#}::Accent;    
    ex:= S = \int{ Q + R +  A \partial{B} C }{x};
    vary(_, $S -> \delta{S}, B -> b$)
    tst:= (\delta{S} = \int{ A \partial{b} C }{x}) - @(ex);
    assert(tst==$0=0$)
    print("Test 09 passed")

test09()

def test10():
    __cdbkernel__=create_scope()
    ex:=x_{m};
    vary(_, $x_{m} -> b_{m}$)
    tst:=b_{m} - @(ex);        
    assert(tst==0)
    print("Test 10 passed")

test10()
        
def test11():
    __cdbkernel__=create_scope()
    \partial{#}::Derivative;
    ex:= A \partial{A C + B} + D A;
    vary(ex, $A->a, B->b, C->c, D->d$)
    tst:= a\partial{A C + B} + A\partial{a C + A c + b} + d A + D a - @(ex);
    assert(tst==0)
    print("Test 11 passed")

test11()

def test12():
    __cdbkernel__=create_scope()
    ex:= A/B + A B;
    vary(ex, $A->a, B->b$)
    tst:= a/B - A/B**2 b + a B + A b - @(ex);
    assert(tst==0)
    print("Test 12 passed")

test12()

# def test13():
#     __cdbkernel__=create_scope()
#     \partial{#}::PartialDerivative;
#     \delta{#}::Accent;
#     x::Coordinate;
#     {A,B}::Depends(x);
#     ex:=\int{ \sqrt{A \partial_{x}{B} + B \partial_{x}{C} } }{x};
#     vary(ex, $B->b$);
#     distribute(_);
#     integrate_by_parts(_, $b$);
#     map_sympy(_, "simplify");

# \partial{#}::PartialDerivative;
# {\mu,\nu,\rho}::Indices;
# \delta{#}::Accent;    
# ex:=  \int{ -1/2 g^{\mu\nu}\partial_{\mu}{\phi} \partial_{\nu}{\phi} + V(\phi) }{x};
# vary(ex, $\phi-> \delta{\phi}$);

def test14():
    __cdbkernel__=create_scope()
    {\mu,\nu}::Indices(vector).
    \delta{#}::Accent.
    \Vt{#}::LaTeXForm("{\tilde{V}}").
    ex:=\int{\Vt_{\mu}\Vt^{\mu}}{x};
    vary(_,$\Vt_{\mu}->\delta{\Vt_{\mu}}$)
    tst:= \int{\delta{\Vt_{\mu}} \Vt^{\mu} + \Vt_{\mu} \delta{\Vt^{\mu}}}{x} - @(ex);
    assert(tst==0)
    print("Test 14 passed")

test14()

def test15():        
    __cdbkernel__=create_scope()
    ex:=A;
    vary(_, $A -> a$)
    assert(ex==$a$)
    print("Test 15a passed")        
    ex:=0;        
    vary(_, $A -> a$)
    assert(ex==0)
    print("Test 15b passed")

test15()
