Author Topic: BCX_ASSERT  (Read 126 times)

MrBcx

  • Administrator
  • Full Member
  • *****
  • Posts: 248
    • View Profile
BCX_ASSERT
« on: January 09, 2020, 11:03:14 PM »
I threw this together today, thinking it might come in handy at times.

There is an included demonstration that should answer most questions.

Basically, you decide where you want to drop these little sanity-savers in your BASIC
source code to later help you isolate bugs in the corresponding C source code.

Code: [Select]
'*****************************************************************
' BCX_ASSERT by Kevin Diggins Jan 2020 ( License: Public Domain )
' BCX_ASSERT is a debugging tool.
'
' It provides a flexible ASSERT capabilty for BCX coders.
' When an Assertion fails, the C source code filename that
' created the executable and the line number within that C
' source code file are displayed, along with an optional
' user-supplied phrase.
'
' The Abortflag is used to control the continued running
' (if possible) of the program after an assertion has failed or,
' if the flag is set, the program will immediately halt.
'
' This is a basic roadmap ... and could be embellished to
' suit your needs -- for example, instead of PRINTING, you
' might direct the information to a Messagebox instead for
' your GUI apps.
'*****************************************************************

$HEADER
#define BCX_ASSERT(b,Txt,Flg)             \
if(!(b))                                  \
{                                         \
printf("\n");                             \
printf("%s%s%d ",__FILE__,":",__LINE__);  \
printf("%s%s ",Txt,"\n");                 \
if(Flg)ExitProcess(0);                    \
}
$HEADER

'*****************************************************************
'                       Demonstration
'*****************************************************************

DIM One$, Two$

One$ = "1111111"   :   Two$ = "22"


BCX_ASSERT (Len(Two$)>Len(One$),"Error: LEN(One$) is > than LEN(Two$)", FALSE)

BCX_ASSERT (1>10, "Error: One is NEVER greater than 10", TRUE)

PRINT "This won't get printed because the 2nd BCX_Assert halts this demo."


dgarner

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: BCX_ASSERT
« Reply #1 on: January 11, 2020, 01:31:59 PM »
This one looks like something I might use a lot, assuming I ever can make time to really engage with BCX again.

Thanks again for sharing.



MrBcx

  • Administrator
  • Full Member
  • *****
  • Posts: 248
    • View Profile
Re: BCX_ASSERT
« Reply #2 on: January 11, 2020, 06:58:58 PM »
Here is a version that uses MessageBox instead of Print, making it suitable for GUI apps.

Code: [Select]

$HEADER
#define BCX_ASSERT(b,Txt,Flg)                                          \
if(!(b))                                                               \
{                                                                      \
char szS[128]={0};                                                     \
sprintf(szS, "%s (in file): %s at line: %d", Txt, __FILE__, __LINE__); \
MessageBox (GetActiveWindow(),szS,"Assert Alert",0 );                  \
if(Flg)ExitProcess(0);                                                 \
}
$HEADER


And the same sample to demonstrate it ...

Code: [Select]

'*****************************************************************
'                       Demonstration
'*****************************************************************

DIM One$, Two$

One$ = "1111111"   :   Two$ = "22"


BCX_ASSERT (Len(Two$)>Len(One$),"Error: LEN(One$) is > than LEN(Two$)", FALSE)

BCX_ASSERT (1>10, "Error: One is NEVER greater than 10", TRUE)

MSGBOX "This won't be displayed because the 2nd BCX_Assert halts this demo."

Wayne Halsdorf

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: BCX_ASSERT
« Reply #3 on: February 14, 2020, 06:19:20 AM »
You could add the for the BCX file and line in the file that generated the code with Modules$[ModuleNdx]  and ModuleLineNos[ModuleNdx]