// channeltest.cc
//
//      Mike O'Donnell
//
//	Simple test case for the message channel problem.
//
//	Two sender threads and two receiver threads compete for a single
//      channel.
//

#include "system.h"
#include "synch.h"

//----------------------------------------------------------------------
// SenderThreadAlph
//----------------------------------------------------------------------

void
SenderThreadAlph(int testChannelInt) {
    Channel *testChannel = (Channel *)testChannelInt;

    printf("--- a -\n");
    testChannel->Send('a');
    printf("--- b -\n");
    testChannel->Send('b');
    printf("--- c -\n");
    testChannel->Send('c');
    printf("--- d -\n");
    testChannel->Send('d');
    printf("--- e -\n");
    testChannel->Send('e');
    printf("--- f -\n");
    testChannel->Send('f');
    printf("--- g -\n");
    testChannel->Send('g');
    printf("--- h -\n");
    testChannel->Send('h');
    printf("--- i -\n");
    testChannel->Send('i');
    printf("--- j -\n");
    testChannel->Send('j');
}

//----------------------------------------------------------------------
// SenderThreadNum
//----------------------------------------------------------------------

void
SenderThreadNum(int testChannelInt) {
    Channel *testChannel = (Channel *)testChannelInt;

    printf("--- - 0\n");
    testChannel->Send('0');
    printf("--- - 1\n");
    testChannel->Send('1');
    printf("--- - 2\n");
    testChannel->Send('2');
    printf("--- - 3\n");
    testChannel->Send('3');
    printf("--- - 4\n");
    testChannel->Send('4');
    printf("--- - 5\n");
    testChannel->Send('5');
    printf("--- - 6\n");
    testChannel->Send('6');
    printf("--- - 7\n");
    testChannel->Send('7');
    printf("--- - 8\n");
    testChannel->Send('8');
    printf("--- - 9\n");
    testChannel->Send('9');
}


//----------------------------------------------------------------------
// ReceiverThreadL
//----------------------------------------------------------------------

void
ReceiverThreadL(int testChannelInt) {
    Channel *testChannel = (Channel *)testChannelInt;

    printf("%c -\n", testChannel->Receive());
    printf("%c -\n", testChannel->Receive());
    printf("%c -\n", testChannel->Receive());
    printf("%c -\n", testChannel->Receive());
    printf("%c -\n", testChannel->Receive());
    printf("%c -\n", testChannel->Receive());
    printf("%c -\n", testChannel->Receive());
    printf("%c -\n", testChannel->Receive());
    printf("%c -\n", testChannel->Receive());
    printf("%c -\n", testChannel->Receive());
}

//----------------------------------------------------------------------
// ReceiverThreadR
//----------------------------------------------------------------------

void
ReceiverThreadR(int testChannelInt) {
    Channel *testChannel = (Channel *)testChannelInt;

    printf("- %c\n", testChannel->Receive());
    printf("- %c\n", testChannel->Receive());
    printf("- %c\n", testChannel->Receive());
    printf("- %c\n", testChannel->Receive());
    printf("- %c\n", testChannel->Receive());
    printf("- %c\n", testChannel->Receive());
    printf("- %c\n", testChannel->Receive());
    printf("- %c\n", testChannel->Receive());
    printf("- %c\n", testChannel->Receive());
    printf("- %c\n", testChannel->Receive());
}

//----------------------------------------------------------------------
// ChannelTest
//----------------------------------------------------------------------

void
ChannelTest() {
    Channel *testChannel = new Channel("testChannel");

    printf("TESTING COMMUNICATION CHANNELS\n\n");

    DEBUG('t', "Entering Channel Test\n");

    Thread *t1 = new Thread("alphabetic sender thread");
    Thread *t2 = new Thread("numeric sender thread");
    Thread *t3 = new Thread("left receiver thread");
    Thread *t4 = new Thread("right receiver thread");

    t1->Fork(SenderThreadAlph, (int)testChannel);
    t2->Fork(SenderThreadNum, (int)testChannel);
    t3->Fork(ReceiverThreadL, (int)testChannel);
    t4->Fork(ReceiverThreadR, (int)testChannel);
}
