lundi 16 juillet 2018

Running unit tests on a non OOP code without any return value

I have a script of this form which is called in a program (from another similar function). I need to use unittest module to write tests for this function.

  • It doesn't exactly return anything but changes a lot of globals
  • It takes inputs
  • I can't change it to an OOP code right now
  • I want to test cases where I change a certain global varible and see if ,let's say, TOTFRAC is positive or something. I have read tests for OOP codes where I call each variable as an object parameter, but what do I do if my code isn't Object Oriented.

Note: I have removed a lot of lines of code because it is rather long, so things might not exactl make sense

import numpy
import math
def SETUP(LAST):
    def GOTO999():
        print(' ERROR IN GAS INPUT : NGAS=',NGAS,'\n')
        for J in range(1,6):
            # print(J)
            print(' N=',J,' NGAS=',NGASN[J],' FRAC=',FRAC[J])
        LAST=1                                                            
        return
    # A lot of globals
    # Initialising variables 
    NBREM=[]
    EBRTOT=[]
    for K in range(1,6):
        NBREM.append(0)
        EBRTOT.append(0.0)


    NGAS=int(input('NGAS'))
    NEVENT=int(input('NEVENT'))
    IMIP=int(input('IMIP'))
    NDVEC=int(input('NDVEC'))
    NSEED=int(input('NSEED'))
    ESTART=float(input('ESTART'))
    ETHRM=float(input('ETHRM'))
    ECUT=float(input('ECUT'))
    ICOUNT=0
    if(IMIP == 1):
        ICOUNT=1 
    if(NGAS == 0):
        LAST=1
        return  
    if(ESTART > 3.0*(10**6) and IMIP == 3):
        print(' SUBROUTINE STOPPED: X-RAY ENERGY=','%.3f' % ESTART,'EV. MAXIMUM ENERGY 3.0MEV')
        sys.exit() 

    if(IMIP != 1 and NEVENT > 10000):
        print(' SUBROUTINE STOPPED: NUMBER OF EVENTS =',NEVENT,' LARGER THAN ARRAY LIMIT OF 10000')
        sys.exit()


    NGASN=[]
    for i in range(1,6):
        NGASN.append(int(input('NGASN'+str(i))))

    FRAC=[]
    for i in range(1,6):
        FRAC.append(round(float(input('FRAC')),4))               
    TEMPC=round(float(input('TEMPC')),4)                         
    TORR=round(float(input('TORR')),4)                           


    # more inputs 
    if(IWRITE != 0):
        outputfile=open("DEGRAD.OUT","w")
    EBIG=0.05*ESTART/1000. 
    EFINAL=ESTART*1.0001+760.0*EBIG/TORR*(TEMPC+ABZERO)/293.15*EFIELD
    if(EFINAL < (1.01*ESTART)):
        EFINAL=1.01*ESTART 
    #   CHECK INPUT
    TOTFRAC=0.00
    if(NGAS == 0 or NGAS > 6):
            GOTO999()
    for J in range(1,NGAS):
        print('J',J)
        if(NGASN[J]== 0 or FRAC[J] == 0.00):
            GOTO999()
        TOTFRAC=TOTFRAC+FRAC[J]

    if(abs(TOTFRAC-100.00)> 1*(10**-6)):
        print(TOTFRAC)
        GOTO999()
    if(NDVEC): #22594
        PHI=0
        THETA=0
    elif(NDVEC==-1):
        PHI=0
        THETA=numpy.arccos(-1)
    elif(NDVEC==0):
        PHI=0.0
        THETA=API/2.0
    elif(NDVEC==2):
        R3=DRAND48(0.0,1.0)
        PHI=TWOPI*R3
        R4=DRAND48(1.5, 1.9)
        THETA=numpy.arccos(1.0-2.0*R4)
    else :
        print('DIRECTION OF BEAM NOT DEFINED NDVEC =',NDVEC)
        sys.exit()

    if(NSEED != 0):
        RM48(NSEED,0,0)                           
    CORR=ABZERO*TORR/(ATMOS*(ABZERO+TEMPC)*100.00)                    


    GOTO999()                                                            
    # end                                                               

Aucun commentaire:

Enregistrer un commentaire