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

CHECK failed, how to use it correctly #407

Closed
Howe2015 opened this issue Apr 16, 2021 · 4 comments
Closed

CHECK failed, how to use it correctly #407

Howe2015 opened this issue Apr 16, 2021 · 4 comments
Labels

Comments

@Howe2015
Copy link

this is my code

  std::string name = "a";
  std::cout <<"the result of std::isalpha(name[0]):"<< std::isalpha(name[0])<<std::endl;
  CHECK(std::isalpha(name[0])) << "name[0]:"<<name[0];

and the code core dump at the log

the result of std::isalpha(name[0]):1024
2021/04/16 16:30:51 317872	CONTRACT [main.cpp->main:66]	
	*******	 EXIT trigger caused by broken Contract: CHECK(std::isalpha(name[0]))
	"name[0]:a
*******	STACKDUMP *******

But when i use this code

  std::string name = "a";
  std::cout <<"the result of std::isalpha(name[0]):"<< std::isalpha(name[0])<<std::endl;
  //CHECK(std::isalpha(name[0])) << "name[0]:"<<name[0];
  CHECK(static_cast<bool>(std::isalpha(name[0]))) << "name[0]:"<<name[0];

it work ok.
How to use "CHECK" correctly.

@KjellKod
Copy link
Owner

Not sure what's going on. Potentially it's some weirdness with your platform.
I've tested this myself and

   std::string name = "a";
  std::cout <<"the result of std::isalpha(name[0]):"<< std::isalpha(name[0])<<std::endl;
  CHECK(std::isalpha(name[0])) << "name[0]:"<<name[0];
  CHECK(static_cast<bool>(std::isalpha(name[0]))) << "name[0]:"<<name[0];
  std::cout << "end of testing: static_cast<bool>(std::isalpha(name[0]): " << static_cast<bool>(std::isalpha(name[0])) << std::endl;

generates, as expected

the result of std::isalpha(name[0]):1
end of testing: static_cast<bool>(std::isalpha(name[0]): 1

@KjellKod
Copy link
Owner

What's your platform, compiler etc?

@KjellKod
Copy link
Owner

Interesting, I see it failing on Windows and Linux but passing on OSX. I.e. the is platform dependent behavior.
For cross platform maybe you should use the tools mentioned https://en.cppreference.com/w/cpp/string/byte/isalpha
I.e. maybe something like std::boolalpha and convert the answer to (bool)std::isalpha(c)

That isalpha is also locale dependent puts also restrictions on how you use the locale so the std::boolalpha would make the code safer.

I consider this a C++ std::library issue and not a g3log issue. I will close this

@Howe2015
Copy link
Author

thanks @KjellKod

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants