      program dstep
      implicit none
      integer :: ierror=0
c
c      J. Mahaffy 2/1/97
c
c
c     Test the vsqwave function for rises at 5 with fall at 7 s.
c     and a rise at 15 seconds with fall at 20 seconds
c
c
      print *, 'Testing function vsqwave'
      call testit( 5.,7.0,ierror)
      call testit (15.,20., ierror)
      if ( ierror.eq.0) then
         print *, 'Function vsqwave is OK'
      else
         print *, '**** Problems with vsqwave, change and retry ****'
      endif
c
      stop
      end
c
c        1         2         3         4         5         6         7
c23456789012345678901234567890123456789012345678901234567890123456789012
c
      subroutine testit(t1,t2,ierror)
c     
c     test the function vsqwave with 4 calls at small time, large time, and
c     times at either side of the step. This assumes that t1>0
c                                          
      implicit none 
      real t1,t2, vsqwave
      integer ierror
      logical near
c
c     John Mahaffy 2/1/97
c     
c     t1 - time at which voltage steps from zero to one
c
c     Check zero time
c
      print *, 'Testing for rise at ',t1, 'seconds, and fall at ',
     $         t2,' seconds'
      if (vsqwave(0.,t1,t2).ne.0) then
      	 print *, '*** sqwave gives wrong result when t=0 ***'
         ierror=ierror+1
      endif
c
      if (vsqwave(t1-.001,t1,t2).ne.0) then
         print *, '*** vsqwave gives wrong result for time just before',
     $            ' step up'
         ierror=ierror+1
      endif
c
      if (.not. near(vsqwave(t1+.001,t1,t2),1.0) ) then
         print *, '*** vsqwave gives wrong result for time just after',
     $           ' step up'
         ierror=ierror+1
      endif
c
      if (.not. near(vsqwave(t2-.001,t1,t2),1.0) ) then
         print *, '*** vsqwave gives wrong result for time just before',
     $           ' step down'
         ierror=ierror+1
      endif
c
      if (vsqwave(t2+.001,t1,t2).ne.0) then
         print *, '*** vsqwave gives wrong result for time just after',
     $            ' step down'
         ierror=ierror+1
      endif
c
      if (vsqwave(t2+1000., t1,t2).ne.0.) then
         print *, '*** vsqwave gives the wrong result at large time'
         ierror=ierror+1
      endif
c
      return
      end
c
      logical function near (var1,var2)
      implicit none
c
c     Checks to see if the real varables var1 and var2 are close to
c     each other.  This uses the Fortran 90 Intrinsic function EPSILON
c     to find the closest that you should expect to represent 1.0
c     with the internal REAL representation used on the variable passed
c     as an argument.
c
c     var1   -   first number to be compared
c     var2   -   second number to be compared
c
      real var1, var2
c
      near = .true.
      if ( var1 .gt. 0.) then
         if (var1.gt.var2*(1.+2*epsilon(var2)).or.
     $       var1.lt.var2*(1.-2*epsilon(var2)) )  near = .false.
       else  if (var1.lt.0.) then
         if (var1.lt.var2*(1.+2*epsilon(var2)).or.
     $       var1.gt.var2*(1.-2*epsilon(var2)) )  near = .false.
       else
         if (var2.gt.epsilon(var2).or. var2.lt.-epsilon(var2))
     $       near = .false.
       endif
       end
c
c        1         2         3         4         5         6         7
c23456789012345678901234567890123456789012345678901234567890123456789012
c
c   Insert your function below this line
c-----------------------------------------------------------------------
      function vsqwave(t,t1,t2)
      implicit none
c
c     Function outputs a square wave with rise at t=t1 and fall at
c     t=t2
c     Result is zero for t<t1 and one for t2>=t>=t1 and zero for t>t2
c
c     t   -   time
c     t1  -   time for the step up from zero to one
c     t2  -   time for step back down to zero
c
c     John Mahaffy 2/1/97
c
      real t,t1,t2,vsqwave
c
      if (t.lt.t1.or. t.gt.t2) then
         vsqwave = 0.
      else
         vsqwave = 1.0
      endif
      return
      end
        
 


     
 
                

