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

Assertions #50

Open
renatocf opened this issue Dec 23, 2015 · 2 comments
Open

Assertions #50

renatocf opened this issue Dec 23, 2015 · 2 comments
Assignees
Labels

Comments

@renatocf
Copy link
Member

Create assertions to test programming, expensive and impossible conditions inside methods.

@renatocf
Copy link
Member Author

renatocf commented Dec 29, 2015

Tasks:

  • Assert default case in HMM / GHMM switch-cases.
  • Assert for null values in methods that return (smart / raw) pointers.
  • Assert for null values in pointer that have (smart / raw) pointer parameters.

@renatocf
Copy link
Member Author

Examples:

  • Switch-case:

    enum class Season { Spring, Summer, Fall, Winter,  };
    enum class Weather { Hot, Cold };
    
    Weather SeasonToWeather(Season season) {
      switch (season) {
        case Season::Spring: // Fall through
        case Season::Summer:
          return Weather::Hot;
        case Season::Fall:   // Fall through
        case Season::Winter:
          return Weather::Cold;
        default:
          assert("Impossible condition in switch-case");
          return {};
      }
    }
  • Number range:

    using Probability = double;
    
    inline bool is_probability(Probability prob) {
      return prob >= 0 && prob <= 1;
    }
    
    Probability drawProbability() {
      static std::mt19937 rng;
      static std::uniform_real_distribution<Probability> distribution(0.0, 1.0);
    
      auto prob = distribution(rng);
      assert(is_probability(prob));
      return prob;
    }
  • Nullity:

    template<typename T>
    inline bool not_null(const std::unique_ptr<T> &ptr) {
      return ptr != nullptr;
    }
    
    template<typename T>
    inline bool not_null(const std::shared_ptr<T> &ptr) {
      return ptr != nullptr;
    }
    
    template<typename Ptr>
    inline bool not_null(const Ptr &ptr) {
      static_assert(std::is_pointer<Ptr>::value, "Should be a pointer type");
      return ptr != nullptr;
    }
    
    class Object {};
    
    std::shared_ptr<Object> createObject() {
      auto created = std::make_shared<Object>();
      assert(not_null(created));
      return created;
    }

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