Reputation: 65
I have a thread that reads data from the console. I want to write a test on it and the only thing I came up with is to declare another stream that will transmit melons through the I/O streams to the first stream, but I do not know how to do it, I want something like this:
void broadcast_func(void* arg){
// run << broadcast
}
TEST_CASE("producer_routine test"){
pthread_t broadcast;
pthread_t run;
if (pthread_create(&broadcast, nullptr, broadcast_func, nullptr) != 0) {
perror("pthread_create() error");
exit(1);
}
if (pthread_create(&run, nullptr, run_func, nullptr) != 0) {
perror("pthread_create() error");
exit(1);
}
}
can someone suggest?
P.S
int run_threads(void* arg){
b_queue bq;
// producer thread
pthread_t p_thr;
if (pthread_create(&p_thr, nullptr, producer_routine, (void*)&bq) != 0) {
perror("pthread_create() error");
exit(1);
}
pthread_detach(p_thr);
...
}
void* producer_routine(void* arg) {
// read data, loop through each value and update the value, notify consumer,
// wait for consumer to process
b_queue* volatile bq = (b_queue*)arg;
int64_t input;
int64_t ch;
while (std::cin >> input, ch = getchar(), (ch != '\n' && ch != EOF)) {
bq->add(input);
// std::cout <<"input: " << input << std::endl;
if (bq->is_full()) {
while (!bq->is_empty())
;
}
}
bq->q_done = true;
return nullptr;
}
Upvotes: 0
Views: 186
Reputation: 67439
Typically you abstract I/O away through a mockable interface, with concrete implementation for production (using console I/O) and for testing (using some other mechanism, perhaps stringstream
or something more custom).
Upvotes: 1