Click here to Skip to main content
15,570,202 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i'm writing a unit test for case sensitivity of rock paper scissors game.
unit tests gonna fail if the first letter of input is small case letter, and it gonna pass if it is upper case letter.


here is my function
char rpsgame(char person1[SIZE], char person2[SIZE]) {
	char player1 = "Player1";
	char player2 = "Player2";
	char draw = "Draw";
	if (strcmp(person1, "Rock") == 0 && strcmp(person2, "Rock") == 0 || strcmp(person1, "Paper") == 0 && strcmp(person2, "Paper") == 0 || strcmp(person1, "Scissor") == 0 && strcmp(person2, "Scissor") == 0) {
		printf("Draw\n");

	}


	// logic for outcome to be draw

	if (strcmp(person1, "Rock") == 0 && strcmp(person2, "Scissor") == 0 || strcmp(person1, "Paper") == 0 && strcmp(person2, "Rock") == 0 || strcmp(person1, "Scissor") == 0 && strcmp(person2, "Paper") == 0) {
		printf("Player1\n");
	}
	// logic for the win of player 1

	if (strcmp(person1, "Rock") == 0 && strcmp(person2, "Paper") == 0 || strcmp(person1, "Paper") == 0 && strcmp(person2, "Scissor") == 0 || strcmp(person1, "Scissor") == 0 && strcmp(person2, "Rock") == 0) {
		printf("Player2\n");
	}
	// logic for the win of player2

	if (strcmp(person1, "Rock") != 0 && strcmp(person1, "Paper") != 0 && strcmp(person1, "Scissor") != 0 && strcmp(person2, "Rock") != 0 && strcmp(person2, "Paper") != 0 && strcmp(person2, "Scissor") != 0) {
		printf("Invalid\n");
	}
}

What I have tried:

// this is my unit test for function
<pre>#include "pch.h"
#include "CppUnitTest.h"
#define SIZE 15
extern "C" char rpsgame(char person1[SIZE], char person2[SIZE]);
using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTest1
{
	TEST_CLASS(UnitTest)
	{
	public:
		
		TEST_METHOD(sensitivity_test)
		{
			char person1[SIZE] = "rock";
			char person2[SIZE] = "paper";
			char outcome[SIZE]="NULL";
			 outcome[SIZE] = rpsgame(person1, person2);
			Assert::AreEqual("player2", outcome);



		}


	};
}
Posted
Updated 7-Nov-21 7:43am
Comments
Richard MacCutchan 7-Nov-21 1:50am    
You cannot assign a string of characters to a char constant:
char player1 = "Player1"; // player1 can only hold a single character

You need to use a pointer type or an array.

Also, as Rick York says below, you need to use a case insensitive comparison in your program.

As I wrote previously, use stricmp.
 
Share this answer
 
If you need case sensitive comparison you need your used strcmp, but if I strongly recommend that you use functions for the comparison like:
C++
BOOL isRock(const char* choice);
and so on.

Your function should have a better interface
C++
char rpsgame(char *choice, char *choice2)


Additional you can check with isupper that the first char is an upper char, BUT any valid choice must be on of the "isXYZ" functions. :-O
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900