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

clock: the test timed out #723

Closed
nozomirin opened this issue Oct 12, 2023 · 4 comments
Closed

clock: the test timed out #723

nozomirin opened this issue Oct 12, 2023 · 4 comments

Comments

@nozomirin
Copy link

This is my solution.

#if !defined(CLOCK_H)
#define CLOCK_H
#include <string>
namespace date_independent {
    class clock {
    public:
        clock(int hour, int minute);  
        static clock at(int hour, int minute);
        operator std::string() const;
        clock& plus(int minutes);
        bool operator==(clock &rhs) noexcept;
        bool operator!=(clock &rhs) noexcept;
    private:
        int hour_;
        int minute_;
    };
}  // namespace date_independent

#endif // CLOCK_H
#include "clock.h"
#include <cmath>
namespace date_independent {
clock::clock(int hour, int minute): hour_(hour), minute_(minute) {
    
}
clock clock::at(int hour, int minute) {
    int actual_hour = ( hour + static_cast<int>(std::floor(minute / 60.0)) ) % 24;
    int actual_minute = minute % 60;
    return clock(actual_hour, actual_minute);
}

clock::operator std::string() const {
    std::string hour = std::to_string(hour_);
    if (hour_ < 10) {
        hour = "0" + hour;
    }
    std::string minute = std::to_string(minute_);
    if (minute_ < 10) {
        minute = "0" + minute;
    } 
    return hour + ":" + minute ;
}

clock& clock::plus(int minutes) {
    int hour = std::floor(minutes / 60.0);
    int minute = minutes % 60;
    hour_ = (hour + hour_ + (minute + minute_) / 60) % 24;
    minute_ = (minute + minute_) % 60;
    return *this;
}

bool clock::operator==(clock &rhs) noexcept {
    return this->hour_ == rhs.hour_ && this->minute_ == rhs.minute_;
}

bool clock::operator!=(clock &rhs) noexcept {
    return !this->operator==(rhs);
}

}  // namespace date_independent

When I ran tests, it timed out.
I tried to comment out these member functions, and I found the at function is the problem. But it seems that there is nothing special in this static member function (I searched other people's solutions and can't find something useful. I even copied someone's code and it just worked. I also paste them in godbolt.org and it compiled.)

Please forgive me if I miss something stupid.

@github-actions
Copy link
Contributor

Hello. Thanks for opening an issue on Exercism. We are currently in a phase of our journey where we have paused community contributions to allow us to take a breather and redesign our community model. You can learn more in this blog post. As such, all issues and PRs in this repository are being automatically closed.

That doesn't mean we're not interested in your ideas, or that if you're stuck on something we don't want to help. The best place to discuss things is with our community on the Exercism Community Forum. You can use this link to copy this into a new topic there.


Note: If this issue has been pre-approved, please link back to this issue on the forum thread and a maintainer or staff member will reopen it.

@nozomirin
Copy link
Author

nozomirin commented Oct 12, 2023

I know why it is not right now. The behavior of the modulo operation in c++ is not the same as python. I'm sorry for the disturbance.

@siebenschlaefer
Copy link
Contributor

Great to hear that you could solve the problem.

For anybody else: I think the reason why the tests do time out is that in this solution the operator== and operator!= cannot be called with const objects. But our testing framework Catch2 does some complex meta-programming magic and the evaluation of REQUIRE(clock1 == clock2); and REQUIRE(clock1 != clock2); causes lots and lots of error messages, too many for our test runner to handle.

@nozomirin
Copy link
Author

Both const for parameter and for function are necessary. It takes me so much time...

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

No branches or pull requests

2 participants