Systemverilog dynamic casting issues - verilog

I've a code snippet like following in my testbench
function void write_to_port( my_data_type_base data );
my_data_type_extended data_ext;
if(!$cast(data_ext, data));
`uvm_error(get_type_name(), "failed to cast");
`uvm_info(get_name(), $psprintf("data_ext :\n%s", data_ext.sprint()), UVM_MEDIUM)
// write data_ext out to another port....
endfunction
When I run it, I'm getting uvm_error as "failed to cast." I'm not quire sure why $cast is not returning 1. As you can see, I'm printing out the extended class data item after casting with uvm_info. I can see that it's being cast properly. If I don't use $cast with if condition, I don't get any runtime error. Isn't it a good coding practice to always use if with dynamic cast to check if $cast is returning 1 ?
What might be the reason behind cast not returning 1 in above case?

I think the semicolon on the line with 'if' does not belong?
I think that consumes the if statement, and then the uvm_error executes regardless of how if evaluates:
if(!$cast(data_ext, data)); <- no semicolon
`uvm_error(get_type_name(), "failed to cast");

Related

Script Runner Error 'Failed type checking'

I am trying to use script runner to be able to check my scripts for any error/deprecated classes. However for a few files simply said the following error:
Failed type checking and we don't know why, it's our fault not yours # line 1, column 1.
I've isolated the problem to the following:
Map tmprc = issue.getCustomFieldValue(rcObj) as Map
if (tmprc) {
// Root Cause Category field defined but ensure both parts of its cascading field are selected
rotCausEntered = ((tmprc.get(null) != null) && (tmprc.get("1") != null))
}
In particular is is the following bit of code causing the error
(tmprc.get(null) != null)
Just curious if anyone knows why this bit of code is causing the error. If I comment the line out everything works fine.
It makes sense that this would fail in a type-checking scenario, since null is not a type and you're passing it to a function get() that expects an Object type parameter. For example, if you run null instanceof Object, it returns false. The type-checker probably doesn't know how to handle that and is returning the error you're seeing.
Why you are using null as a key in a Map? Changing that behavior is probably your solution.

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito

getting the error
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
1 matchers expected, 2 recorded.
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
at
when(
getProgramService
.callService(any(GetProgramType.class)))
.thenReturn(jAXBresponse);
please explain the error and possible resolution
The code you posted looks fine; I don't see a problem with it. It is likely the code immediately before or above it causing an exception. The key is here:
Invalid use of argument matchers! 1 matchers expected, 2 recorded.
any, as you used it above, doesn't return a "special kind of null" or "special kind of GetProgramType"; those don't exist. Instead, it returns null and as a side effect puts a matcher on a stack. When checking for matchers, Mockito looks at the stack, and checks that it is either empty (i.e. check equality with all arguments) or exactly equal to the number of arguments in the call you're checking (i.e. there is a matcher for each argument).
What's happening here is that you're getting one more matcher than the callService method expects. I see this often enough when developers mistakenly try to save a matcher in a local variable:
String expectedString = Matchers.anyString(); // DOESN'T WORK
// Instead, this just adds a matcher to the stack, almost certainly where it
// doesn't belong.
...or mock a final method:
// Assume getProgramService.otherMethod is final.
when(getProgramService.otherMethod(anyString())).thenReturn(123L);
// This actually calls getProgramService.otherMethod(null) and leaves an
// anyString matcher on the stack without setting any expectation that
// would returns 123L.
To confirm this is a problem, temporarily add this statement immediately before the one you posted:
Mockito.validateMockitoUsage();
This artificially checks the stack for matchers, and will likely throw an exception on that line. If it does, check the code above your post. Either way, adding some surrounding code to your question will likely help us debug it.

CodeContracts: Invoking this method will always lead to an error

Still learning code contracts. When I create a small test, I get the following message from the checker: CodeContracts: Invoking this method will always lead to an error. If this is wanted, consider adding Contract.Requires(false) to document it.
I don't understand what it is try to tell me. How would I add Contract.Requires(false) to this example so the warning is not shown?
This is the code. Note that this is a contrived example solely for the purpose of learning CC.
void DoSomething(object test) {
Contract.Requires(test != null);
MessageBox.Show(test.ToString());
}
void InvokeDoSomething() {
DoSomething(null);
}
Code Contracts have discovered that you
Require a parameter to a method to never be null
Literally call it with null
CC is basically telling you that your code will always fail. The contract seems fine, but your call is bad. The solution is of course not to add Contract.Requires(false) but to not call the method with null.

XSD validation with XML reader, collecting validation errors. (C#)

I'm currently fighting with using an XMLSerializer to execute XSD validation and collect the validation errors in the files. The task is the validation of the file, based on custom XSD-s containing valueset information, presence information etc.
My problem is the following: when using the XMLReader it stops at the first error, if we attach a listener to the ValidationEvents of the reader (through XMLReaderSettings). So I simply catch the exception where I log the error. So far everything is fine, the problems start to appear after logging the exception. Right after that the XMLReader goes to the end tag of the failed field, but I cannot validate the next field due to an unexplained exception.
To put it in practice, here's my code where I catch the exception:
private bool TryDeserialize(XmlSerializer ser, XmlReader read,out object item)
{
string Itemname = read.Name;
XmlReader read2 = read.ReadSubtree();
try
{
item= ser.Deserialize(read2);
return true;
}
catch (Exception e)
{
_ErrorList.Add("XSD error at " + Itemname + ": " + e.InnerException.Message);
item = null;
return false;
}
}
This routine works well, but what follows is problematic. Assume I pass the following XML snippet to this code:
<a>2885</a>
<b>ABC</b>
<c>5</c>
Assume that 'b' may not have 'ABC' as a value, so I get an XSD error. At the end of this, the xmlreader will be at
'EndElement, Name=b'
from which I simply cannot move unless I get an exception. If I do xmlreader.read, then I get the following exception (cut the namespace here):
"e = {"The element 'urn:iso:.....b' cannot contain child element 'urn:iso:.....:c' because the parent element's content model is text only."}"
After this the xmlreader is at 'Element, Name=c', so it seems good, but when trying to deserialize it with the code above, I get the following exception:
'_message = "The transition from the 'ValidateElement' method to the 'ValidateText' method is not allowed."'
I don't really see how I may go over it. I tried without a second reader reading the subtree, but I have the same problem. Please suggest me something, I really am stuck. Thanks a lot in advance!
Greets
You may have to consider the following things:
In general, it is not always possible to "collect" all the errors, simply because validating parsers are free to abandon the validation process when certain types of errors occur, particularly those that put the validator in a state where it can't reliably recover. For e.g., a validator may still continue after running into a constraining facet violation for a simple type, but it'll skip a whole section if it runs in unexpected content.
Unlike parsing into a DOM, where the loading of a DOM is not affected by a validating reader failing validation, deserializing into an object is (or at least should be) totally different: DOM is about being well formed; deserializing, i.e. strong typing is about being valid.
Intuitively I would think that if you get a validation error, what is the point in continuing with the deserialization, and further validation?
Try validating your XML independent of deserialization. If indeed you get more errors flagged with this approach, then the above should explain why. If not, then you're chasing something else.

Using constructor to load data in subsonic3?

I'm getting an error while trying to load an record through the constructor.
The constructor is:
public Document(Expression<Func<Document,bool>> expression);
and i try to load a single item in like this
var x = new Document(f=>f.publicationnumber=="xxx");
publicationnumber isn't a key but tried making an it an unique key and still no go..
Am i totally wrong regarding the use of the constructor? and can someone please tell me how to use that constructor?
The error i'm getting is:
Test method TestProject1.UnitTest1.ParseFileNameTwoProductSingleLanguage threw exception: System.NullReferenceException:
with the following stacktrace:
SubSonic.Query.SqlQuery.Where[T](Expression1` expression)
Load`[T]`(T item, Expression1expression)
db.Document..ctor(Expression``1 expression) in C:\#Projects\DocumentsSearchAndAdmin\DocumentsSearchAndAdmin\Generated\ActiveRecord.cs: line 5613
rest removed for simplicity
Regards
Dennis
Use == instead of =, i.e.:
...(f=>f.publicationnumber == "xxx");
I've just gotten the SubSonic source, and found out that it had to with the expression parser and my lack of knowledge thereof .. my right side of the expression was actually an item in an string array - and s[PUBNO] (PUBNO is a const) and it was looking for an column named s instead of publicationnumber, i don't know if this i a bug or not in the linq classes
none the less - i've managed to get it to work by creating a local variable containing the value of s[PUBNO] and using that instead...
//dennis

Resources