how to make these testcases code nice? [closed] - c++

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am a freshman of TDD and C++, I write some code works but it seems ugly...
could you give me some tips on how to refactor these code?
I define testcases like this:
// define some test case
namespace test_case_planes {
const std::string t_filename = "planes.segy_first_trace";
boost::uintmax_t t_file_size = 5888;
boost::uintmax_t t_traces_size = 1;
boost::int16_t t_trace_samples_size = 512;
boost::int16_t t_sample_interval = 4000; // 2000us, 2ms
}
namespace test_case_ld0042_file_00018 {
const std::string t_filename = "ld0042_file_00018.sgy_first_trace";
boost::uintmax_t t_file_size = 12040;
boost::uintmax_t t_traces_size = 1;
boost::int16_t t_trace_samples_size = 2050;
boost::int16_t t_sample_interval = 2000; // 2000us, 2ms
}
and test like this(with google test)
TEST(Segy, constructWithNoParas){
using namespace test_case_planes;
segy::Segy* test_segy = new segy::Segy();
EXPECT_EQ(test_segy->getFilename(), t_filename);
}
TEST(Segy, constructWithFilename){
using namespace test_case_ld0042_file_00018;
segy::Segy* test_segy = new segy::Segy(t_filename);
EXPECT_EQ(test_segy->getFilename(), t_filename);
}
TEST(Segy, setFilename){
using namespace test_case_ld0042_file_00018;
segy::Segy* test_segy = new segy::Segy();
test_segy->setFilename(t_filename);
EXPECT_EQ(test_segy->getFilename(), t_filename);
}

I think what you are looking for is a fixture. I am not familiar with google test, but test fixtures should be available in most test frameworks. You should look into the documentation for that.

Related

C++ code: count not working [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I wanted to ask, i formulated this code to solve a question, but count does not seem to provide the right value.
Any advice.Any help appreciated. Thanks.
#include<iostream.h>
#include<conio.h>
void main()
{
int count;
for(int a=1;a<125;a++)
for(int m=1;m<125;m++)
for(int n=1;n<125;n++)
{
if(a*(m+n+2)==249-m)
{
cout<<"a = "<<a<<" m = "<<m<<" n = "<<n<<"\n";
count=count+1;
}
}
cout<<"count = "<<count<<"\n";
getch();
}
You do not init the count. Remember to set int count = 0;.
Your compiler will warn you about this and save you the trouble of debugging or asking if you only let it. (from #chris)

Loop without for, while or goto [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
In Windows, one can use structured exception handling in C to write a pseudo-loop that prints all numbers from 1 to 1000 like this:
int n = 0;
__try {
*(int *)0 = 0;
}
__except(printf("%i\n", ++n), n < 1000 ? -1 : 1) {
}
I wonder if there are also other ways in C/C++ to create a loop that isn't trivial to detect if you search the code for the usual suspect keywords for, while and goto.
In C++, a simple lambda can do that:
std::function<void(int,int)> print = [&](int from, int to)
{
std::cout << from << " ";
if ( from < to ) print(++from, to);
};
print(1, 1000);
It will print all integers from 1 to 1000. And it doesn't use for, while or goto.

replace function in std::string giving problems in C++ [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am using C++ string functions in cocos2dx. I have the following string CorrectAns = "below".
for(int i = 0; i<CorrectAns.size();i++)
{
CorrectAns.replace(i,i,"?");
}
This function should return my string as "?????", but its returning only 4 charcters ie "????".
When I write like this,
for(int i = 0; i<CorrectAns.size();i++)
{
if(i == 0)
{
CorrectAns.replace(i,i,"?");
}
}
It just crashes.
and works fine only when I write it as " CorrectAns.replace(i,i+1,"?");"
Why is the function working this way?? Can anyone help me please??
string& replace ( size_t pos1, size_t n1, const string& str );
For the versions with parameters pos1 and n1, the section replaced
begins at character position pos1 and spans for n1 characters within
the string.
So you should use
for(int i = 0; i<CorrectAns.size();i++)
{
CorrectAns.replace(i,1,"?");
}
Mb it will be more usefull use something like
CorrectAns.assign(CorrectAns.size(), '?');

What is the check_union256d? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
What is the check_union256d function?
It's placed in following code:
/* { dg-do run } */
/* { dg-require-effective-target avx } */
/* { dg-options "-O2 -mavx" } */
#include "avx-check.h"
void static
avx_test (void)
{
int i;
union256d u, s1, s2;
double e [4];
s1.x = _mm256_set_pd (2134.3343,1234.635654,453.345635,54646.464356);
s2.x = _mm256_set_pd (41124.234,2344.2354,8653.65635,856.43576);
u.x = _mm256_div_pd (s1.x, s2.x);
for (i = 0; i < 4; i++)
e[i] = s1.a[i] / s2.a[i];
if (check_union256d (u, e))
abort ();
}
It's from Intel AVX which is:
a new 256 bit instruction set extension to SSE and is designed for applications that are Floating Point (FP) intensive.

error message with this line [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Why do i get this error message?
Error: expected a ';'
from:
int main()
{
int score;
double distance;
char playAgain;
bool shieldsUp;
short lives;
short aliensKilled;
score = 0
distance = 1200.76;
playAgain = 'y';
shieldsUp = true;
lives = 3
aliensKilled = 10;
the error is under distance = 1200.76; and aliensKilled = 10;,
You have:
score = 0
You want:
score = 0;
Similar for lives = 3
You really forgot something:
...
score = 0;
...
lives = 3;
...