Guest User

Untitled

a guest
Nov 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <exception>
  2. #include <stdexcept>
  3. #include <vector>
  4.  
  5. #include <assert.h>
  6. #include <stddef.h>
  7.  
  8. // Returns a munged/hash of an int.
  9. // Always returns something different than what is passed in.
  10. int munge(int i){
  11. // We can't munge zero, it would still be zero, so that's an exception!
  12. if(i == 0)
  13. throw std::runtime_error("You're screwed now.");
  14. return (i >> 2) ^ (i << 7);
  15. }
  16.  
  17. class frobulator{
  18. std::vector<int> m_data0, m_data1;
  19. public:
  20. void add(int i){
  21. // We always add to both the vectors at once, so it's safe!
  22. m_data0.push_back(i);
  23. m_data1.push_back(munge(i));
  24.  
  25. // Munging should always return different data.
  26. assert(m_data0.back() != m_data1.back());
  27. }
  28.  
  29. void dump(){
  30. // We know the vectors are the same size, so it's safe!
  31. for(size_t i = 0; i < m_data0.size(); i++){
  32. printf("%i -> %i\n", m_data0[i], m_data1[i]);
  33. }
  34. }
  35. };
  36.  
  37. int main(int argc, char **argv){
  38. frobulator f;
  39. for(size_t i = 0; i < 37; i++){
  40. try{
  41. f.add(i);
  42. }
  43. catch(...){
  44.  
  45. }
  46. }
  47.  
  48. // Watch the fireworks. Or maybe it's just a whimper.
  49. f.dump();
  50. }
Add Comment
Please, Sign In to add comment