Question
In C Code ============================================================================ utilsTester.c #include #include #include #include #include #include #include #include #include colorUtils.h /** * For CMYK applications, accuracy only needs to be
In C Code
============================================================================
utilsTester.c
#include #include #include #include #include #include #include #include
#include "colorUtils.h"
/** * For CMYK applications, accuracy only needs to be to within * 0.01, but other applications may require finer accuracy. */ const double EPSILON = 0.01;
/** * For randomized cyclical testing, this determines the number of * tests to run */ const int RANDOM_TEST = 1000;
/** * This function is used to determine if two floating * point numbers are "close enough" to each other based * on a small EPSILON value */ static int isClose(double x, double y) { return fabs(x - y)
/** * assert that for any random r, g, b, converting from RGB->CMYK->RGB * doesn't change the values */ static void testRandomCyclicalEquality(void **state) { srandom(time(NULL));
for (int i = 0; i
rgbToCMYK(r, g, b, &c, &m, &y, &k); cmykToRGB(c, m, y, k, &r, &g, &b);
// have to subtract 1 to allow for minor rounding differences assert_true( (r == origR || r == origR - 1) && (g == origG || g == origG - 1) && (b == origB || b == origB - 1) ); } }
/** * This function tests rgbToCMYK's error handling of NULL * values for its pass-by-reference parameters. Each of * the four parameters are tested independently. The function * should return a non-zero error value so we assert that the * return value is not equal to zero. * */ static void testRgbToCmykNull(void **state) { double c, m, y, k; assert_int_not_equal(rgbToCMYK(0,0,0,NULL,&m,&y,&k), 0); assert_int_not_equal(rgbToCMYK(0,0,0,&c,NULL,&y,&k), 0); assert_int_not_equal(rgbToCMYK(0,0,0,&c,&m,NULL,&k), 0); assert_int_not_equal(rgbToCMYK(0,0,0,&c,&m,&y,NULL), 0);
}
/** * This function tests rgbToCMYK's error handling of out-of-range * values for the r, g, b parameters, each tested independently. * two values are tested each: a less-than-zero value and a value * greater than 255. */ static void testRgbToCmykOutOfBounds(void **state) { double c, m, y, k; assert_int_not_equal(rgbToCMYK(-1,127,127,&c,&m,&y,&k), 0); assert_int_not_equal(rgbToCMYK(256,127,127,&c,&m,&y,&k), 0); assert_int_not_equal(rgbToCMYK(127,-1,127,&c,&m,&y,&k), 0); assert_int_not_equal(rgbToCMYK(127,256,127,&c,&m,&y,&k), 0); assert_int_not_equal(rgbToCMYK(127,127,-1,&c,&m,&y,&k), 0); assert_int_not_equal(rgbToCMYK(127,127,256,&c,&m,&y,&k), 0); }
/** * This function tests rgbToCMYK passing a single, hard-coded * rgb-value (Steele Blue, 70, 130, 180). */ static void testRgbToCmyk001(void **state) { double c, m, y, k; //steel blue: rgbToCMYK(71,130,181,&c,&m,&y,&k); assert_true( isClose(c, .61) && isClose(m, .28) && isClose(y, 0.0) && isClose(k, .29) ); }
/** * This function tests cmykToRGB's error handling of NULL * values for its pass-by-reference parameters. Each of * the three parameters are tested independently. The function * should return a non-zero error value so we assert that the * return value is not equal to zero. * */ static void testCmykToRgbNull(void **state) { int r, g, b; assert_int_not_equal(cmykToRGB(0,0,0,0,NULL,&g,&b), 0); assert_int_not_equal(cmykToRGB(0,0,0,0,&r,NULL,&b), 0); assert_int_not_equal(cmykToRGB(0,0,0,0,&r,&g,NULL), 0);
}
/** * This function tests cmykToRGB's error handling of out-of-range * values for the c, m, y, k parameters, each tested independently. * two values are tested each: a less-than-zero value and a value * greater than 255. */ static void testCmykToRgbOutOfBounds(void **state) { int r, g, b; assert_int_not_equal(cmykToRGB(-1, .5, .5, .5, &r,&g,&b), 0); assert_int_not_equal(cmykToRGB( 2, .5, .5, .5, &r,&g,&b), 0); assert_int_not_equal(cmykToRGB(.5, -1, .5, .5, &r,&g,&b), 0); assert_int_not_equal(cmykToRGB(.5, 2, .5, .5, &r,&g,&b), 0); assert_int_not_equal(cmykToRGB(.5, .5, -1, .5, &r,&g,&b), 0); assert_int_not_equal(cmykToRGB(.5, .5, 2, .5, &r,&g,&b), 0); assert_int_not_equal(cmykToRGB(.5, .5, .5, -1, &r,&g,&b), 0); assert_int_not_equal(cmykToRGB(.5, .5, .5, 2, &r,&g,&b), 0); }
/** * This function tests cmykToRGB passing a single, hard-coded * cmyk-value (Steele Blue, .61, .28, 0, .29). */ static void testCmykToRgb001(void **state) { int r, g, b; //steel blue: cmykToRGB(.61, .28, 0, .29, &r, &g, &b); assert_true( r == 71 && g == 130 && b == 181 ); }
/** * This function is a generic testing function for rgbToCMYK * in which the passed state is expected to have 7 double values * corresponding to 3 RGB input values and 4 CMYK output values * that are known to be equivalent. * */ static void testRgbToCmykValues(void **state) { //cast the generic state to a double array double *values = *((double **)state); double c, m, y, k; rgbToCMYK( (int) values[0], (int) values[1], (int) values[2], &c, &m, &y, &k); assert_true( isClose(c, values[3]) && isClose(m, values[4]) && isClose(y, values[5]) && isClose(k, values[6]) ); }
/** * This function is a generic testing function for cmykToRGB * in which the passed state is expected to have 7 double values * corresponding to 3 RGB output values and 4 CMYK input values * that are known to be equivalent. * */ static void testCmykToRgbValues(void **state) { //cast the generic state to a double array double *values = *((double **)state); int r, g, b; cmykToRGB(values[3], values[4], values[5], values[6], &r, &g, &b); assert_true( r == values[0] && g == values[1] && b == values[2] ); }
/** * Takes a set of values where the first three are a color in RGB * and the next 4 are CMYK. These are directed to the RBG->CMYK and * CMYK->RGB functions */ static void testBidirectionalValues(void **state) { testRgbToCmykValues(state); testCmykToRgbValues(state); }
int main(int argc, char **argv) { // values to test bi-directionally double aquamarine[] = {128, 255, 212, 0.5, 0, 0.17, 0}; double salmon[] = {250, 127, 115, 0.0, 0.49, 0.54, 0.02}; double huskerRed[] = {209, 0, 0, 0.0, 1, 1, 0.18}; double cycleBlue[] = { 40, 159, 209, 0.81, 0.24, 0, 0.18}; double orange[] = {255, 158, 33, 0, 0.38, 0.87, 0}; double facebookBlue[] = { 60, 90, 153, 0.61, 0.41, 0, 0.4}; double twitterBlue[] = { 56, 160, 242, 0.77, 0.34, 0, 0.05}; double instagramPink[] = {194, 43, 163, 0, 0.78, 0.16, 0.24}; double powerpointOrange[] = {209, 69, 36, 0, 0.67, 0.83, 0.18}; double spotifyGreen[] = { 30, 214, 96, 0.86, 0, 0.55, 0.16};
const struct CMUnitTest tests[] = { cmocka_unit_test(testRandomCyclicalEquality), cmocka_unit_test(testRgbToCmykNull), cmocka_unit_test(testRgbToCmykOutOfBounds), cmocka_unit_test(testRgbToCmyk001), cmocka_unit_test(testCmykToRgbNull), cmocka_unit_test(testCmykToRgbOutOfBounds), cmocka_unit_test(testCmykToRgb001),
cmocka_unit_test_prestate(testBidirectionalValues, &aquamarine), cmocka_unit_test_prestate(testBidirectionalValues, &salmon), cmocka_unit_test_prestate(testBidirectionalValues, &huskerRed), cmocka_unit_test_prestate(testBidirectionalValues, &cycleBlue), cmocka_unit_test_prestate(testBidirectionalValues, &orange), cmocka_unit_test_prestate(testBidirectionalValues, &facebookBlue), cmocka_unit_test_prestate(testBidirectionalValues, &twitterBlue), cmocka_unit_test_prestate(testBidirectionalValues, &instagramPink), cmocka_unit_test_prestate(testBidirectionalValues, &powerpointOrange), cmocka_unit_test_prestate(testBidirectionalValues, &spotifyGreen) };
return cmocka_run_group_tests(tests, NULL, NULL);
}
=====================================================================
makefile
# # makefile for color utilities C library # assumes cmocka (a unit testing framework) is installed on your # system #
CC = gcc FLAGS = -Wall -std=gnu99 INCLUDES = -l cmocka -lm
utilsTester: colorUtils.o utilsTester.c $(CC) $(FLAGS) colorUtils.o utilsTester.c -o utilsTester $(INCLUDES)
colorUtils.o: colorUtils.c colorUtils.h $(CC) $(FLAGS) -c colorUtils.c -o colorUtils.o $(INCLUDES)
clean: rm -f *~ *.o
Problem Statement In this hack you'll get some more practice writing functions that utilize pass-by-reference (pointers), error handling and enumerated types. There are several different ways to model colors including RGB and CMYK. RGB is generally used in displays and models a color with three values in the range (0,255] corresponding to the red, green and blue Hack 6.0 - Computer Science I 1/3 "contribution" to the color. For example, the triple (255, 255,0) corresponds to a full red and green (additive) value which results in yellow. CMYK or Cyan-Magenta-Yellow- Black is a model used in printing where four colors of ink are combined to make various colors. In this system, the four values are on the scale [0, 1]. Write functions to convert between these models. 1. Write a function to convert from an RGB color model to CMYK. To convert to CMYK, you first need to scale each integer value to the range [0, 1] by simply computing W = 6 and then using the following formulas: k=1 max{r',.',V} (1 pl - k) (1 - k) (1-- k) (1 - k) _(1-W-k) (1 - k) Your function should have the following signature: int rgbTOCMYK (int r, int g, int b, double *c, double *m, double *y, double *k) Identify any and all error conditions and use the return value to indicate an error code (0 for no error, non-zero value(s) for error conditions). 2. Write a function to me the following formulas. Q - 95% + 2/3 - 9=255 - (1 - m). (1 - k) 2. Write a function to convert from CMYK to RGB using the following formulas. r=255. (1-c). (1 - k) q=255. (1 -m).(1 - k) b=255. (1 - y).(1 - k) Results should be rounded with any values exceeding 255 reset to 255. Your function should have the following signature: int cmykToRGB (double c, double m, double y, double k, int *r, int *g, int *b) Identify any and all error conditions and use the return value to indicate an error code (0 for no error, non-zero value(s) for error conditions). Instructions You are encouraged to collaborate any number of students before, during, and after your scheduled hack session. Design at least 3 test cases for each function before you begin designing or imple- menting your program. Test cases are input-output pairs that are known to be correct using means other than your program. You may (in fact are encouraged) to define any additional "helper functions that you find useful. Include the name(s) of everyone who worked together on this activity in your source file's header. Place your prototypes and documentation in a header file named colorUtils.h and your source in a file named colorUtils.c. A testing file, utilsTester.c has been provided that uses cmocka (https:// cmocka.org/), a unit testing framework for C. We have already written several (17) test cases for you. Using these examples, implement your test cases using cmocka for your two functions. You should have at least 3 test cases for each function for a total of 23 test cases. Problem Statement In this hack you'll get some more practice writing functions that utilize pass-by-reference (pointers), error handling and enumerated types. There are several different ways to model colors including RGB and CMYK. RGB is generally used in displays and models a color with three values in the range (0,255] corresponding to the red, green and blue Hack 6.0 - Computer Science I 1/3 "contribution" to the color. For example, the triple (255, 255,0) corresponds to a full red and green (additive) value which results in yellow. CMYK or Cyan-Magenta-Yellow- Black is a model used in printing where four colors of ink are combined to make various colors. In this system, the four values are on the scale [0, 1]. Write functions to convert between these models. 1. Write a function to convert from an RGB color model to CMYK. To convert to CMYK, you first need to scale each integer value to the range [0, 1] by simply computing W = 6 and then using the following formulas: k=1 max{r',.',V} (1 pl - k) (1 - k) (1-- k) (1 - k) _(1-W-k) (1 - k) Your function should have the following signature: int rgbTOCMYK (int r, int g, int b, double *c, double *m, double *y, double *k) Identify any and all error conditions and use the return value to indicate an error code (0 for no error, non-zero value(s) for error conditions). 2. Write a function to me the following formulas. Q - 95% + 2/3 - 9=255 - (1 - m). (1 - k) 2. Write a function to convert from CMYK to RGB using the following formulas. r=255. (1-c). (1 - k) q=255. (1 -m).(1 - k) b=255. (1 - y).(1 - k) Results should be rounded with any values exceeding 255 reset to 255. Your function should have the following signature: int cmykToRGB (double c, double m, double y, double k, int *r, int *g, int *b) Identify any and all error conditions and use the return value to indicate an error code (0 for no error, non-zero value(s) for error conditions). Instructions You are encouraged to collaborate any number of students before, during, and after your scheduled hack session. Design at least 3 test cases for each function before you begin designing or imple- menting your program. Test cases are input-output pairs that are known to be correct using means other than your program. You may (in fact are encouraged) to define any additional "helper functions that you find useful. Include the name(s) of everyone who worked together on this activity in your source file's header. Place your prototypes and documentation in a header file named colorUtils.h and your source in a file named colorUtils.c. A testing file, utilsTester.c has been provided that uses cmocka (https:// cmocka.org/), a unit testing framework for C. We have already written several (17) test cases for you. Using these examples, implement your test cases using cmocka for your two functions. You should have at least 3 test cases for each function for a total of 23 test casesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started