Javassist: Generate "assert" statement - assert

I try to add an "assert" statement to a method.
But I get this exception:
Caused by: compile error: assert(boolean) not found in mypackage.MyClassThatIsInstrumented
at javassist.compiler.TypeChecker.atMethodCallCore(TypeChecker.java:716)
at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:681)
at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:156)
at javassist.compiler.ast.CallExpr.accept(CallExpr.java:45)
at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:241)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:329)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:350)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
at javassist.compiler.CodeGen.atMethodBody(CodeGen.java:291)
at javassist.compiler.Javac.compileBody(Javac.java:222)
at javassist.CtBehavior.setBody(CtBehavior.java:360)
... 30 more
Any ideas how to solve that?

A workaround is to implement the assertion as a conditional:
if(Foo.class.desiredAssertionStatus() && expr) {
throw new AssertionError();
}
This is basically equivalent to
assert expr;
Now it is a matter of how you would like to inject it. If you need to assert against the method parameters, you can use method.insertBefore(); if you're asserting the result, you can use method.insertAfter().
If the statement needs to be inserted into the method's body, somewhere in between the statements, then you can use ExprEditor to match the required statement where you want this new statement to be injected

Related

Standard ML multiple condition statement

I am about finished with a script I am writing but I have one last condition statement to add to my function.
fun whileloop (x:real,a:int,b:real) =
if (a<1)
then (x,a,b)
else whileloop(x+1.0,a-1,b-1.0)
This is my current loop I have created. It is basically accomplishing everything I need under one exception. I want it to exit its loop once the b variable hits zero[if this happens before a reaches zero). I believe Standard ML will not let me do a condition statement for a real variable...such as b<1.0. just to give you an idea of what I am trying to accomplish...I want the following code to work below:
fun whileloop (x:real,a:int,b:real) =
if (a<1 or b<1.0)
then (x,a,b)
else whileloop(x+1.0,a-1,b-1.0)
of course this code does not work due to the syntax and a condition statement being checked against a real number...but how could I accomplish this task while keeping my skeleton somewhat intact. I simply want to add another if condition statement to the existing skeleton. In C++ this was a fairly simple task.
Here is the answer. Thanks to John Coleman.
fun whileloop (x:real,a:int,b:real) =
if (a<1 orelse b<1.0)
then (x,a,b)
else whileloop(x+1.0,a-1,b-1.0)

What is the oposite of EXPECT_CALL?

I have some expectations like EXPECT_CALL (...)
EXPECT_CALL(t1, foo()).Times(1);
I want to create the oposite.
I expect that a certain function won't be executed.
What is the method I should use?
Something like EXPECT_NOT_CALL (...) ?
In GTest something similar to EXPECT_NOT_CALL doesn't exist however, there are several options to receive this behaviour:
1.Create a StrictMock. In StrictMock any unexpected called cause a failure.
2.Use .Times(0):
EXPECT_CALL(t1, foo()).Times(0);
In this option you use the count mechanism however, it checks if the value is 0.(so any call leads a failure...)
3.Use method 2 and create a Macro:
#define EXPECT_NOT_CALL(a,b) EXPECT_CALL(a, b).Times(0);

Assert statement in Verilog

I'm completely new to Verilog, so bear with me.
I'm wondering if there is an assert statement in Verilog. In my testbench, I want to be able to assert that the outputs of modules are equal to certain values.
For example,
mymodule m(in, out);
assert(out == 1'b1);
Googling gave me a few links, but they were either too complex or didn't seem to be what I wanted.
There is an open source library for assertions called OVL. However, it's pretty heavy. One trick I nicked from there is creating a module to do assertions.
module assert(input clk, input test);
always #(posedge clk)
begin
if (test !== 1)
begin
$display("ASSERTION FAILED in %m");
$finish;
end
end
endmodule
Now, any time you want to check a signal, all you have to do is instantiate an assertion in your module, like this:
module my_cool_module(input clk, ...);
...
assert a0(.clk(clk), .test(some_signal && some_other_signal));
...
endmodule
When the assertion fails, you'll get a message like this:
ASSERTION FAILED in my_cool_module.a0
The %m in the display statement will show the entire hierarchy to the offending assertion, which is handy when you have a lot of these in a larger project.
You may wonder why I check on the edge of the clock. This is subtle, but important. If some_signal and some_other_signal in the expression above were assigned in different always blocks, it's possible the expression could be false for a brief period of time depending on the order that your Verilog simulator schedules the blocks (even though the logic was entirely valid). This would give you a false negative.
The other thing to note above is that I use !==, which will cause the assertion to fail if the test value is X or Z. If it used the normal !=, it could silently give a false positive in some cases.
Putting the above together with a macro works for me:
`define assert(signal, value) \
if (signal !== value) begin \
$display("ASSERTION FAILED in %m: signal != value"); \
$finish; \
end
Then later in my test module:
initial begin // assertions
#32 `assert(q, 16'hF0CB)
end
As an example test fail case:
ASSERTION FAILED in test_shift_register: q != 16'hF0CB
you can write like this
if(!(out==1'b1)) $finish;
If your simulator supports SystemVerilog syntax, there is an assert keyword which does what you want.
Verilog doesn't support assertions. Some tools support PSL, which places the assertions in comments but this is non-standard. You should consider using hierarchical references from a testbench instead otherwise you have to place each assertion in a process which will get messy.
The easiest way to mimic C-like assertions is probably a `define since this will make them global.
`define assert(condition) if(condition) begin $finish(1); end
In order to check signals in a non-procedural context, such as your example, you will need a different macro that builds a condition signal and then triggers a test event for that signal.
`define assert_always(condition) generate if(1) begin wire test = condition; always #(test) `assert(condition) end endgenerate
The generate above will create a new scope for the variable test so multiple instances should work.
A better way in a procedural might be to create a task in a separate file and then include that in any module declaration.
task assert(input condition);
if(!condition)
$finish(2);
endtask
For non-procedural contexts you'll need to create a module containing a process and instance that module. This will require a unique name for each instance unless you put it in a generate block.

Regex for JavaScript source

XPages application fails with following stack trace:
com.ibm.jscript.InterpretException: Script interpreter error, line=30, col=43: 'component' is null
at com.ibm.jscript.ASTTree.ASTMember.interpret(ASTMember.java:153)
at com.ibm.jscript.ASTTree.ASTCall.interpret(ASTCall.java:88)
at com.ibm.jscript.ASTTree.ASTBlock.interpret(ASTBlock.java:100)
at com.ibm.jscript.ASTTree.ASTIf.interpret(ASTIf.java:85)
at com.ibm.jscript.ASTTree.ASTBlock.interpret(ASTBlock.java:100)
at com.ibm.jscript.ASTTree.ASTIf.interpret(ASTIf.java:85)
at com.ibm.jscript.ASTTree.ASTBlock.interpret(ASTBlock.java:100)
at com.ibm.jscript.ASTTree.ASTTry.interpret(ASTTry.java:109)
at com.ibm.jscript.ASTTree.ASTIf.interpret(ASTIf.java:85)
at com.ibm.jscript.ASTTree.ASTProgram.interpret(ASTProgram.java:119)
at com.ibm.jscript.ASTTree.ASTProgram.interpretEx(ASTProgram.java:139)
From this I know, that there is problem with variable "component" nested inside hierarchy of blocks:
if -> try -> { -> if -> { -> if -> { -> method call with invalid argument.
I don't know what to look for exactly, search for "component" yields too many results.
What regex should I use to find the right spot based on code hierarchy?
In this case I see a good chance that you have not put all your SSJS code into try/catch blocks. The bad news: searching the cause of this error is extremely cumbersome as close to all SSJS blocks may be the root cause of this error.
For that reason I placed my own rule (and ignore it every now and then) to put EVERY SSJS block into a try/catch like this:
try {
// ... do fancy stuff here
} catch (e) {
print(e.toString());
}
The toString() call is used for some special cases where the error object appears to no automatically convert into an object that can be handled by the print method.
If it is the case, you have not put all SSJS blocks into a try/catch, this is exactly the right time to do so and keep that coding pattern for the future. It really helps every now and then ;-)
Instead of printStackTrace and toString() , you could just say print(e), which will output only the error message(should be the same as e.message). The error object if passed to a java routine, you could get the error line.
variable "component" nested inside hierarchy of blocks ==> We have made this working without issues.

Regular Expression Search in Visual Studio 2010

I'm not sure if this is possible within VS, but I'm working with a massive VB.NET file that needs to log every function call for debug purposes. Problem is, that not every function has the Log command in it. I'm trying to use RegEx to find the function definitions that do not have a log within them.
This would NOT be a match:
Public Function Test1() as Boolean
Log.Tracelog("Test1()")
Return True
End Function
This WOULD be a match:
Public Function Test2() as Boolean
Return False
End Function
The closest I've come is using the following:
(function|sub|property) .*\n.*~(Log\.t)
In my own mind, it should work, but no matter how I word it, it's still pulling every function as a match, even those that have the "Log.Tracelog" call in the function.
Is there anyway I can search to find the latter case?
Try this:
(function|sub|property) .*\n~(.*Log\.t)
I moved .* from just before the ~() (preventmatch) to just inside it.
Why not use the debug.WriteLine methods for the functions you want logged. You can also use the stack to get the method name:
Private Function test1() As Boolean
Debug.WriteLine(New System.Diagnostics.StackTrace().GetFrame(0).GetMethod.Name)
Return False
End Function
Then the messages only output when debugging and only in the methods you want.