Skip to content

Commit

Permalink
feat: add OPARAM entry enableAssertOpcodes for enabling assert opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
hlolli committed Dec 19, 2022
1 parent 537df12 commit 20f9da6
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 32 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ set(libcsound_SRCS
InOut/winEPS.c
InOut/circularbuffer.c
OOps/aops.c
OOps/assert_ops.c
OOps/bus.c
OOps/cmath.c
OOps/diskin2.c
Expand All @@ -767,7 +768,6 @@ set(libcsound_SRCS
OOps/schedule.c
OOps/sndinfUG.c
OOps/str_ops.c
OOps/test_ops.c
OOps/ugens1.c
OOps/ugens2.c
OOps/ugens3.c
Expand Down Expand Up @@ -1441,7 +1441,7 @@ set (CPACK_PACKAGE_VERSION_PATCH "0")
include (CPack)

# Documentation
if(BUILD_DOCS)
if(BUILD_DOCS)
find_package(Doxygen)
if(DOXYGEN_FOUND)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.doxygen
Expand Down
10 changes: 8 additions & 2 deletions Engine/entry1.c
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,14 @@ OENTRY opcodlst_1[] = {
(SUBR) strrindex_opcode, (SUBR) strrindex_opcode, NULL },
{ "print_type", S(PRINT_TYPE_OP),0, 1, "", ".",
(SUBR) print_type_opcode, NULL, NULL },
{ "assert", S(ASSERT_OP),0, 1, "", "i",
(SUBR) assert_opcode, NULL, NULL },
{ "assert_true.i", S(ASSERT_OP),0, 1, "", "b",
(SUBR) assert_true_opcode, NULL, NULL },
{ "assert_true.k", S(ASSERT_OP),0, 1, "", "B",
(SUBR) assert_true_opcode, NULL, NULL },
{ "assert_false.i", S(ASSERT_OP),0, 1, "", "b",
(SUBR) assert_false_opcode, NULL, NULL },
{ "assert_false.k", S(ASSERT_OP),0, 1, "", "B",
(SUBR) assert_false_opcode, NULL, NULL },
#ifdef HAVE_CURL
{ "strfromurl", S(STRCPY_OP), 0, 1, "S", "S", (SUBR) str_from_url },
#endif
Expand Down
4 changes: 2 additions & 2 deletions H/test_ops.h → H/assert_ops.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
test_ops.h:
assert_ops.h:
This file is part of Csound.
Expand Down Expand Up @@ -29,7 +29,7 @@ extern "C" {

typedef struct {
OPDS h;
MYFLT *boolean;
int32_t *boolean;
} ASSERT_OP;


Expand Down
6 changes: 3 additions & 3 deletions H/entry1.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
#include "midiinterop.h"
#include "linevent.h"
#include "str_ops.h"
#include "test_ops.h"
#include "assert_ops.h"
#include "bus.h"
#include "pstream.h"
#include "remote.h"
Expand Down Expand Up @@ -503,5 +503,5 @@ int32_t coef2parm(CSOUND *csound, void *p);
int32_t resonbnk_init(CSOUND *csound, void *p);
int32_t resonbnk(CSOUND *csound, void *p);
int32_t schedule_array(CSOUND *csound, void *p);
int32_t assert_opcode(CSOUND *csound, ASSERT_OP *p);

int32_t assert_true_opcode(CSOUND *csound, ASSERT_OP *p);
int32_t assert_false_opcode(CSOUND *csound, ASSERT_OP *p);
34 changes: 27 additions & 7 deletions OOps/test_ops.c → OOps/assert_ops.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
test_ops.c:
assert_ops.c:
This file is part of Csound.
Expand All @@ -20,14 +20,34 @@
*/

#include "csoundCore.h"
#include "test_ops.h"
#include "assert_ops.h"

/* increments csound->perferrcnt for false (0) */
int32_t assert_opcode(CSOUND *csound, ASSERT_OP *p)
int32_t assert_true_opcode(CSOUND *csound, ASSERT_OP *p)
{
if (*p->boolean == ((MYFLT) 0.0)) {
csound->perferrcnt += 1;
}
if (!csound->oparms->enableAssertOpcodes) {
return OK;
}

int32_t value = (int32_t) *p->boolean;
if (value == 0) {
csound->perferrcnt += 1;
return NOTOK;
}

return OK;
}

int32_t assert_false_opcode(CSOUND *csound, ASSERT_OP *p)
{
if (!csound->oparms->enableAssertOpcodes) {
return OK;
}

int32_t value = (int32_t) *p->boolean;
if (value == 1) {
csound->perferrcnt += 1;
return NOTOK;
}

return OK;
}
12 changes: 9 additions & 3 deletions Top/argdecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ static const char *longUsageList[] = {
Str_noop("--aft-zero set aftertouch to zero, not 127 (default)"),
Str_noop("--limiter[=num] include clipping in audio output"),
Str_noop("--vbr set MPEG encoding to variable bitrate"),
Str_noop("--enable-assert run csound with assertion opcodes enabled"),
Str_noop(" and print out a report of failed assertions at the end,"),
Str_noop(" by default the assert opcodes are ignored."),
" ",
Str_noop("--help long help"),
NULL
Expand Down Expand Up @@ -1256,11 +1259,14 @@ static int decode_long(CSOUND *csound, char *s, int argc, char **argv)
return 1;
}
else if (!(strcmp(s, "vbr"))) {
#ifdef SNDFILE_MP3
#ifdef SNDFILE_MP3
O->mp3_mode = SF_BITRATE_MODE_VARIABLE;
#endif
#endif
return 1;
}
} else if (!(strncmp(s, "enable-assert", 13))) {
O->enableAssertOpcodes = 1;
return 1;
}
csoundErrorMsg(csound, Str("unknown long option: '--%s'"), s);
return 0;
}
Expand Down
19 changes: 10 additions & 9 deletions Top/csound.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,15 +937,16 @@ static const CSOUND cenviron_ = {
(char*) NULL, (char*) NULL, NULL,
(char*) NULL, (char*) NULL, (char*) NULL,
(char*) NULL, (char*) NULL,
0, /* midiKey */
0, /* midiKeyCps */
0, /* midiKeyOct */
0, /* midiKeyPch */
0, /* midiVelocity */
0, /* midiVelocityAmp */
0, /* noDefaultPaths */
1, /* numThreads */
0, /* syntaxCheckOnly */
0, /* midiKey */
0, /* midiKeyCps */
0, /* midiKeyOct */
0, /* midiKeyPch */
0, /* midiVelocity */
0, /* midiVelocityAmp */
0, /* noDefaultPaths */
1, /* numThreads */
0, /* syntaxCheckOnly */
0, /* enableAssertOpcodes */
1, /* useCsdLineCounts */
0, /* samp acc */
0, /* realtime */
Expand Down
9 changes: 5 additions & 4 deletions include/csoundCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ typedef struct CORFIL {
from files */
int numThreads;
int syntaxCheckOnly;
int enableAssertOpcodes;
int useCsdLineCounts;
int sampleAccurate; /* switch for score events sample accuracy */
int realtime; /* realtime priority mode */
Expand Down Expand Up @@ -423,7 +424,7 @@ typedef struct CORFIL {
} TABDAT;

#define MAX_STRINGDAT_SIZE 0xFFFFFFFF

typedef struct {
char *data;
size_t size;
Expand Down Expand Up @@ -893,10 +894,10 @@ typedef struct CORFIL {
int kperf_debug(CSOUND *csound);

/*
check if code is running at init time.
check if code is running at init time.
result may not be valid in realtime mode
*/
int csoundIsInitThread(CSOUND *csound);
*/
int csoundIsInitThread(CSOUND *csound);

#endif /* __BUILDING_LIBCSOUND */

Expand Down
21 changes: 21 additions & 0 deletions tests/unit/utest1.csd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<CsoundSynthesizer>
<CsOptions>
--enable-assert
</CsOptions>
<CsInstruments>
sr=44100
ksmps=1
nchnls=1
instr 1
i1 = 1 + 1
assert_true(i1 == 2)
assert_false(i1 == 3)
endin
</CsInstruments>
<CsScore>
i1 0 0
e
</CsScore>
</CsoundSynthesizer>

0 comments on commit 20f9da6

Please sign in to comment.