Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test): add opcode assert for testing #1663

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion 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 Down Expand Up @@ -1440,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
8 changes: 8 additions & 0 deletions Engine/entry1.c
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +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_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
40 changes: 40 additions & 0 deletions H/assert_ops.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
assert_ops.h:

This file is part of Csound.

The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/

#ifndef CSOUND_TEST_OPS_H
#define CSOUND_TEST_OPS_H

#ifdef __cplusplus
extern "C" {
#endif


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


#ifdef __cplusplus
}
#endif

#endif /* CSOUND_TEST_OPS_H */
4 changes: 3 additions & 1 deletion H/entry1.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "midiinterop.h"
#include "linevent.h"
#include "str_ops.h"
#include "assert_ops.h"
#include "bus.h"
#include "pstream.h"
#include "remote.h"
Expand Down Expand Up @@ -502,4 +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_true_opcode(CSOUND *csound, ASSERT_OP *p);
int32_t assert_false_opcode(CSOUND *csound, ASSERT_OP *p);
53 changes: 53 additions & 0 deletions OOps/assert_ops.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
assert_ops.c:

This file is part of Csound.

The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/

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

int32_t assert_true_opcode(CSOUND *csound, ASSERT_OP *p)
{
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
5 changes: 5 additions & 0 deletions include/csound.h
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,11 @@ extern "C" {
*/
PUBLIC void csoundSetHostData(CSOUND *, void *hostData);

/**
* Returns the total error count of the current performance.
*/
PUBLIC int csoundErrCnt(CSOUND *csound);

/**
* Set a single csound option (flag). Returns CSOUND_SUCCESS on success.
* NB: blank spaces are not allowed
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
25 changes: 25 additions & 0 deletions tests/c/csound_orc_compile_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,31 @@ TEST_F (OrcCompileTests, testReuse)
csoundPerform(csound);
}

TEST_F (OrcCompileTests, testAssert)
{
int result;
char* instrument =
"instr 1 \n"
"assert(0) \n"
"assert(0) \n"
"assert(0) \n"
"assert(0) \n"
"assert(0) \n"
"assert(0) \n"
"assert(1) \n"
"endin \n";

result = csoundCompileOrc(csound, instrument);
ASSERT_TRUE(result == 0);
result = csoundReadScore(csound, "i 1 0 0\n");
ASSERT_TRUE(result == 0);
result = csoundStart(csound);
ASSERT_TRUE(result == 0);
csoundPerform(csound);
csoundStop(csound);
ASSERT_EQ (6, csoundErrCnt(csound));
}

TEST_F (OrcCompileTests, testLineNumber)
{
char* instrument =
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>