What does EXPECT_HRESULT_SUCCEEDED means in googletest? - c++

I have found 4 googletest which are
ASSERT_HRESULT_SUCCEEDED(expression);
EXPECT_HRESULT_SUCCEEDED(expression);
ASSERT_HRESULT_FAILED(expression);
EXPECT_HRESULT_FAILED(expression);
my question is, what does this tests do according to proper expression. What will be the response in which kind of expression? Can you explain with example??

What I think is when we need to test something in customized form: EXPECT_HRESULT_SUCCEEDED(exp)
Example:
EXPECT_HRESULT_SUCCEEDED((number % 2 == 0) && (number <= 10) && (number >= 0));
unlike others:
EXPECT_EQ(), EXPECT_GT() ... which does only a specific check condition.

Related

What am I misunderstanding when determining if a value is between two numbers?

I have a PDF form that adds up several different answers and displays the sum of these answer at the bottom of the page. I want to take that number, and have a sentence underneath it display three different options depending on the sum.
Greater than 60: Proceed
between 45 & 60: Consult Sales Lead
Less than 45: Decline
I am attempting to run a custom calculation script that takes the sum (which is named "total") and writes the above options, but I'm running into a myriad of errors.
My code I've written is below
var A = this.getField("total").value;
if (A >= 60){
event.value = "Proceed";
} else {
if (A <= 45){
event.value = "Decline";}
} else {
if (A < 60 && A > 45){
event.value = "Proceed, decision made with sales leader";}
}
If I were to only write the below block, I do not get any errors.
var A = this.getField("total").value;
if (A >= 60){
event.value = "Proceed";
}
I'm a newbie when it comes to most JavaScript, so any help is greatly appreciated! Thanks in advance!
I have based most of my code off of different search results from google. My main source
example 1
Below are a few other links I've referenced
example 2
example 3
You can leave away the
if (A < 60 && A > 45) {
line, because that condition is always true after the two previous conditions.
And then, it should work properly.
Just if you want to stay with that last condition, you would have to close it with a curly brace.
I think I managed to figure it out! For some reason, the addition of the "else" factor was causing syntax errors, so I tried it without and it seems to work as intended!
My code, for anyone that happens to find this, is the following:
var A = this.getField("total").value;
if (A >= 60) {event.value = "Proceed";}
if (A <= 59 && A >= 46) {event.value = "Proceed, decision made with sales leader";}
if (A <= 45) {event.value = "Decline";}
if (A == 0) {event.value = " ";}
Thanks to everyone that took a look at this, even if you didn't get to comment before I figured it out!

Can I use return to return to the start of an if statement?

I'm currently programming some homework, and we have to make a program that turns a hindu-arabic numeral into a roman numeral. I've already made the first part, where my teacher said we had to make sure the number is in between 1 and 3999. My algorithm so far is like this:
if (num-1000) > 0 {
add M to output
num -= 1000
return if
}
else {
(repeat for other digits, aka 500, 100, 50, and so on)
}
The problem is, I don't know if it's even possible. All the Stack Overflow pages I've seen say that I should use while statements for this, but since we haven't tackled while statements yet (even though I've self-learned it) we can't use while loops. So, can I use return to return to the start of an if statement?
What you are describing is a while. You can also achieve that with a go to but using that at this stage of learning would inspire some very very bad habits so I wholeheartedly discourage it. Another way you could do this is with recursion but that is an even more advance topic.
Given your restrictions (no loops, a number between 1 and 3999) I think you are supposed to use a bunch of ifs. Something like this pseudocode:
if (n >= 3000)
add 'M'
else if (n >= 2000)
add 'MM'
else if (n >= 1000)
add 'MMM'
n = n % 1000;
if (n >= 900)
add 'CM'
// and so on

Find a logical expression that is true if `n` is a multiple of `2019` and is not in the interval `(a, b)`

I had the task of finding a logical expression that would result in 1 if and only if a given number n is a multiple of 2019 and is NOT from the interval (a, b).
The textbook gave the following answer and I don't really understand it:
a>=n || b<=n && (n%3==0 && n%673==0)
The thing between those parantheses I understand to be equivalent to n%2019==0, so that's alright. But I don't understand why this works, I mean the && operator has higher priority that the || operator, so wouldn't we evaluate
b<=n && (n%3==0 && n%673==0)
first and only at the end if n<=a? I thought that if I were to do it, I would do it like this:
(a>=n || b<=n) && (n%3==0 && n%673==0)
So I just added that extra set of parantheses. Now we would check if the number is not in the interval (a, b), then we would check if it is a multiple of 2019 and then we would 'and' those to answers to get the final answer. This makes sense to me. But I don't understand why they omitted that set of parantheses, why would that still work? Shouldn't we consider that && has higher priority than ||, so we add an extra set of parantheses? Would it still work? Or is it me that is wrong?
Trying it out shows that the expression as written without the extra parentheses doesn't work:
bool expr(int n, int a, int b)
{
return a>=n || b<=n && (n%3==0 && n%673==0);
}
expr(1000, 2000, 2018) for example evaluates to true, even though it is not a multiple of 2019.
As you pointed out, the logical AND operator && has higher precedence than the logical OR operator || (reference), so the expression is equivalent to:
a>=n || (b<=n && (n%3==0 && n%673==0))
which is always true when n <= a, even if it's not a multiple of 2019.
A clearer expression would be:
(n % 2019 == 0) && (n <= a || n >= b)

What is the operator precedence of this if statement? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Improve this question
I'm staring at the following expression, written by someone else:
if (variableA == CaseA && GetCase(variableB) == CaseA && GetCase(variableC) == CaseA && !CaseB(variableB) || variableA != CaseA)
{
// do stuff
}
What's really tripping me up is this bit:
!CaseB(variableB) || variableA != CaseA
Because there aren't any parentheses to group the conditionals together.
How will the C++ compiler evaluate these if statements? If one of the AND operators evaluates to false, does this mean that the OR won't be checked due to short circuit?
Edit
Wow, I guess the haters really want to hate today, considering the amount of downvotes for each answer in addition to the -4 (and counting?) for the question. And, uh, no actual explanation for why? Lol.
Stay classy, people. Stay classy. ;)
For the record, simply stating that there's a "left to right evaluation" doesn't really say much in terms of providing a valid answer for the question. I actually did use the great power of Google (which also lead to a few posts here that I read) before posting.
I'll admit my mistake here was failing to mention that, and while I think that the C++ reference is the most natural link to post as a supplement for an answer, the best information I was able to get was inadvertently from here, via the following quote in a source code example:
|| has lower precedence than &&
Which is arguably vague, because "lower precedence" isn't actually specified in terms of what that means in this context.
Either way, to those who actually contributed you will receive an upvote - I'm going to accept the answer which gave me what I was really looking for.
The expression is equivalent to:
( (variableA == CaseA) &&
(GetCase(variableB) == CaseA) &&
(GetCase(variableC) == CaseA) &&
(!CaseB(variableB))
)
||
(variableA != CaseA)
The expression will be evaluated from the top down (because of the short circuit rules), and if any of the first four lines return false, none of the remain first four lines will be evaluated. If (and only if) one of the first four lines return false, then the final line will be evaluated.
But I am interested that the last line is the negation of the first
Operator && has higher precedence than operator || as seen here so all of the && will occur first, then lastly the ||, in other words if I added parentheses
if ((variableA == CaseA && GetCase(variableB) == CaseA && GetCase(variableC) == CaseA && !CaseB(variableB)) || variableA != CaseA)
So basically
if (all_the_ands || variableA != CaseA)
If the left hand side of that expression is true, the right hand side will not execute due to short circuiting.
http://en.cppreference.com/w/cpp/language/operator_precedence
The precedence is as listed in the answer by Sachin Goyal (and as listed more clearly in the above link).
The specific precedence detail that seems to be confusing you is that the ! associates directly to CaseB(variableB) not to anything larger.
The sequence is left to right across each && or || with short circuiting.
If the expression to the left of the !CaseB(variableB) || variableA != CaseA you asked about is false then the !CaseB(variableB) is skipped and it evaluates variableA != CaseA. But if the earlier expression is true, it uses the value of !CaseB(variableB) to decide whether to evaluate the rest.
The combination of initial test variableA == CaseA with final test variableA != CaseA seems confused. It might mean what was intended, but could have been done more simply.
When you code (X && Y && Z) the meaning is ((X && Y) && Z) but because of the way && acts, that is the same as (X && (Y && Z)). So variableA == CaseA acts like a guard on the rest of the operands of the &&s, so that when that is false you skip all the way to the other side of the || and make the opposite test and end up resolving true. So it would have been simpler to get the same effect by just starting with variableA != CaseA ||
In your example , operator precedence is as below .
! > == > && > ||
So , first statement evaluated would be
1) !CaseB(variableB) only if all other "&&" condition happens to be true .If any of "variableA == CaseA && GetCase(variableB) == CaseA && GetCase(variableC) == CaseA " is false then short circuting will happen here and "!CaseB(variableB)" won't be executed .
After that ,
2) if condition 1) is true , again short circuiting( as you have correctly guessed ) else "||" condition will be evaluated .
You are right , if condition 2) happens to be true , other or(||) condition won't even be checked due to short circuit .

Is it possible to define a variable in expression in C++?

I have this insane homework where I have to create an expression to validate date with respect to Julian and Gregorian calendar and many other things ...
The problem is that it must be all in one expression, so I can't use any ;
Are there any options of defining variable in expression? Something like
d < 31 && (bool leapyear = y % 4 == 0) || (leapyear ? d % 2 : 3) ....
where I could define and initialize one or more variables and use them in that one expression without using any ;?
Edit: It is explicitly said, that it must be a one-line expression. No functions ..
The way I'm doing it right now is writing macros and expanding them, so I end up with stuff like this
#define isJulian(d, m, y) (y < 1751 || (y == 1752 && (m < 9) || (m == 9 && d <= 2)))
#define isJulianLoopYear(y) (y % 4 == 0)
#define isGregorian(d, m, y) (y > 1573 || (y == 1752 && (m > 9) || (m == 9 && d > 13)))
#define isGregorianLoopYear(y) ((y % 4 == 0) || (y % 400 = 0))
// etc etc ....
looks like this is the only suitable way to solve the problem
edit: Here is original question
Suppose we have variables d m and y containing day, month and year. Task is to write one single expression which decides, if date is valid or not. Value should be true (non-zero value) if date is valid and false (zero) if date is not valid.
This is an example of expression (correct expression would look something like this):
d + 4 == y ^ 85 ? ~m : d * (y-2)
These are examples of wrong answers (not expressions):
if ( log ( d ) == 1752 ) m = 1;
or:
for ( i = 0; i < 32; i ++ ) m = m / 2;
Submit only file containing only one single expression without ; at the end. Don't submit commands or whole program.
Until 2.9.1752 was Julian calendar, after that date is Gregorian calendar
In Julian calendar is every year dividable by 4 a leap year.
In Gregorian calendar is leap year ever year, that is dividible by 4, but is not dividible by 100. Years that are dividable by 400 are another exception and are leap years.
1800, 1801, 1802, 1803, 1805, 1806, ....,1899, 1900, 1901, ... ,2100, ..., 2200 are not loop years.
1896, 1904, 1908, ..., 1996, 2000, 2004, ..., 2396,..., 2396, 2400 are loop years
In september 1752 is another exception, when 2.9.1752 was followed by 14.9.1752, so dates 3.9.1752, 4.9.1752, ..., 13.9.1752 are not valid.
((m >0)&&(m<13)&&(d>0)&&(d<32)&&(y!=0)&&(((d==31)&&
((m==1)||(m==3)||(m==5)||(m==7)||(m==8)||(m==10)||(m==12)))
||((d<31)&&((m!=2)||(d<29)))||((d==29)&&(m==2)&&((y<=1752)?((y%4)==0):
((((y%4)==0)&&((y%100)!=0))
||((y%400)==0)))))&&(((y==1752)&&(m==9))?((d<3)||(d>13)):true))
<evil>
Why would you define a new one, if you can reuse an existing one? errno is a perfect temporary variable.
</evil>
I think the intent of the homework is to ask you to do this without using variables, and what you are trying to do might defeat its purpose.
In standard C++, this is not possible. G++ has an extension known as statement expressions that can do that.
I don't believe you can, but even if you could, it would only have scope inside of the parentheses they are defined in (in your example) and cannot be used outside of them.
Your solution, which I will not provide fully for you, will probably go along these lines:
isJulian ? isJulianLeapyear : isGregorianLeapyear
To make it more specific, it could be like this:
isJulian ? (year % 4) == 0 : ((year % 4) == 0 || (year % 400) == 0)
You'll have to just make sure your algorithm is correct. I'm not a calender expert, so I wouldn't know about that.
First: Don't. It may be cute, but even if there's an extension that allows it, code golf is a dangerous game that will almost always end up causing mmore grief than it solves.
Okay, back to the 'real' question as defined by the homework. Can you make additional functions? If so, instead of capturing whether or not it's a leap year in a variable, make a function isLeapYear(int year) that returns the correct value.
Yes, that means you'll calculate it more than once. If that ends up being a performance issue, I'd be incredibly surprised... and it's a premature optimization to worry about that in the first place.
I'd be very surprised if you weren't allowed to write functions as part of doing this. It seems like that'd be half the point of an exercise like this.
......
Okay, so here's a quick overview of what you'll need to do.
First, basic verification - that month, day, year are possible values at all - month 0-11 (assuming 0-based), day 0-30, year non-negative (assuming that's a constraint).
Once you're past that, I'd probably check for the 1752 special cases.
If that's not relevant, the 'regular' months can be handled pretty simply.
This leaves us with the leap year cases, which can be broken down into two expressions - whether something is a leap year (which will be broken down additionally based on gregorian/julian), and whether the date is valid at that point.
So, at the highest level, your expression looks something like this:
areWithinRange(d,m,y) && passes1752SpecialCases(d,m,y) && passes30DayMonths(d,m,y) && passes31DayMonths(d,m,y) && passesFebruaryChecks(d,m,y)
If we assume that we only return false from our sub-expressions if we actively detect a rule break (31 days in June for the 30DayMonth rule returns false, but 30 days in February is irrelevant and so passes true), then we can pretty much say that the logic at that level is correct.
At this point, I'd write separate functions for the individual pieces (as pure expressions, a single return ... statement). Once you've gotten those in place, you can replace the method call in your top-level expression with the expanded version. Just make sure you parenthesize (is that a word?) everything sufficiently.
I'd also make a test harness program that uses the expression and has a number of valid and invalid inputs, and verifies that you're doing the right thing. You can write that in a function for ease of cut and paste for the final turn-in by doing something like:
bool isValidDate(int d, int m, int y)
{
return
// your expression here
}
Since the expression will be on a line by itself, it'll be easy to cut and paste.
You may find other ways to simplify your logic - excepting the 1752 special cases, days between 1 and 28 are always valid, for instance.
Considering it is homework, I think the best advice would be an approach to deriving your own solution.
If I were tackling this assignment, I would start by breaking the rules.
I would write a c++ function that given the variables d, m, and y, returns a boolean result on the validity of the date. I would use as many non-recursive helper functions as needed, and feel free to use if, else if, and else, but no looping aloud.
I would then Inline all helper functions
I would reduce all if, else if, and else statement to ? : notation
If I was successful at limiting my use of variables, I might be able to reduce all this to a single function with no variables - the body of which will contain the single expression I seek.
Good Luck.
You clearly have to have the date passed in somehow. Beyond that, all you're really going to be doing is chaining && and || (assuming we get the date as a tm struct):
#include <ctime>
bool validate(tm date)
{
return (
// sanity check that all values are positive
date.tm_mday >= 1 && date.tm_mon >= 0 && date.tm_year >= 0
// check for valid days
&& ((date.tm_mon == 0 && date.tm_mday <= 31)
|| (date.tm_mon == 1 && date.tm_mday <= (date.tm_year % 4 ? 28 : 29))
|| (date.tm_mon == 2 && date.tm_mday <= 31)
// yadda yadda for the other months
|| (date.tm_mon == 11 && date.tm_mday <= 31))
);
}
The parenthesis around date.tm_year % 4 ? 28 : 29 actually aren't needed, but I'm including them for readability.
UPDATE
Looking at a comment, you'll also need similar rules to validate dates that don't exist in the Gregorian calendar.
UPDATE II
Since you're dealing with dates in the past you will need to implement a more correct leap year test. However, I generally deal with dates in the future, and this incorrect leap year test will give correct results in 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, 2048, 2052, 2056, 2060, 2064, 2068, 2072, 2076, 2080, 2084, 2088, 2092, and 2096. I will make a prediction that before this test fails in 2100 silicon-based computers will be forgotten relics. I seriously doubt we will use C++ on the quantum computers in use then. Besides, I won't be the programmer assigned to fix the bug.