import unittest
from gradescope_utils.autograder_utils.decorators import number, weight
import subprocess

class TestDiff(unittest.TestCase):
    def setUp(self):
        pass 

    # Associated test number within Gradescope
    @number("0")
    # Associated point value within Gradescope
    @weight(5)
    def test_Compile(self):
        # Title used by Gradescope 
        """Clean Compile"""

        # Create a subprocess to run the students make file to ensure it compiles
        test = subprocess.Popen(["make"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output = test.stderr.read().strip().decode('utf-8')
        test.kill()

        # Standard unit test case with an associated error message
        self.assertTrue(output == "", msg=output)
        test.terminate()
        
    # Associated test number within Gradescope
    @number("1")
    # Associated point value within Gradescope
    @weight(15)
    def test_Header(self):
        # Title used by Gradescope 
        """Check that PPM header information is correct"""

        # Create a subprocess to run the students code to obtain an output
        cat = subprocess.Popen(["cat", "input/15.txt"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        test = subprocess.Popen(["./lab6.out"], stdin=cat.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output = test.stdout.read().strip().decode('utf-8')
        test.kill()
        
        # Boolean to hold value for test pass/fail (updated procedurally)
        correct = True

        # Set msg to blank string, in case test passes
        msg = ''
        
        # Array of expected lines of output, line by line in expected order
        expected = ['P3', '15', '7', '255']
        
        # Check if header information is correct
        header = output.split()
        if header[0] != expected[0]:
            correct = False
            msg = ['Your PPM image\'s header label is incorrect. Check the lab instructions for the correct file format label.']
        elif header[1] != expected[1]:
            correct = False
            msg = ['Your PPM image\'s width is incorrect. Make sure your program uses the width entered by the user for your PPM image.']
        elif header[2] != expected[2]:
            correct = False
            msg = ['Your PPM image\'s height is incorrect. Check the lab instructions for where to find the correct height.']
        elif header[3] != expected[3]:
            correct = False
            msg = ['Your PPM image\'s maximum pixel value is incorrect. Check the lab instructions for the maximum pixel value of a PPM image.']
            
        # Pass test pass/fail status and message to test run output
        self.assertTrue(correct, msg)
        test.terminate()
        
    # Associated test number within Gradescope
    @number("2")
    # Associated point value within Gradescope
    @weight(20)
    def test_Width15(self):
        # Title used by Gradescope 
        """Check that PPM image with width 15 is created"""

        # Create a subprocess to run the students code to obtain an output
        cat = subprocess.Popen(["cat", "input/15.txt"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        test = subprocess.Popen(["./lab6.out"], stdin=cat.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output = test.stdout.read().strip().decode('utf-8')
        test.kill()
        
        # Boolean to hold value for test pass/fail (updated procedurally)
        correct = True

        # Set msg to blank string, in case test passes
        msg = ''
        
        # Array of expected pixels in PPM output
        expected = [('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0')]
        
        # Split program output from header information to pixels
        encoding, height, width, max, *values = output.split()
        # Create array of PPM pixels
        rgb = [tuple(values[i:i+3]) for i in range(0, len(values), 3)]

        # Check that length of rgb array is correct, if yes then loop through pixels until a wrong one is found
        if len(rgb) < len(expected):
            correct = False
            msg = ['Your PPM image contains fewer pixels than expected. Check that your for loops are printing enough pixels for the specified width, and that your height is calculated correctly.']
        elif len(rgb) > len(expected):
            correct = False
            msg = ['Your PPM image contains more pixels than expected. Check that your for loops are printing exactly as many pixels as necessary for the specified width, and that your height is calculated correctly.']
        else:
            index = 0
            for pixel in rgb:
                if pixel != expected[index]:
                    correct = False
                    index = index + 1
                    msg = [f'Pixel #{index} in your PPM image is incorrect. Fix this pixel and try again.']
                    break
                index = index + 1
        
        # Pass test pass/fail status and message to test run output
        self.assertTrue(correct, msg)
        test.terminate()
        
    # Associated test number within Gradescope
    @number("3")
    # Associated point value within Gradescope
    @weight(20)
    def test_Width42(self):
        # Title used by Gradescope 
        """Check that PPM image with width 42 is created"""

        # Create a subprocess to run the students code to obtain an output
        cat = subprocess.Popen(["cat", "input/42.txt"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        test = subprocess.Popen(["./lab6.out"], stdin=cat.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output = test.stdout.read().strip().decode('utf-8')
        test.kill()
        
        # Boolean to hold value for test pass/fail (updated procedurally)
        correct = True

        # Set msg to blank string, in case test passes
        msg = ''
        
        # Array of expected pixels in PPM output
        expected = [('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('0', '128', '0'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '255', '255'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0'), ('255', '165', '0')]

        # Split program output from header information to pixels
        encoding, height, width, max, *values = output.split()
        # Create array of PPM pixels
        rgb = [tuple(values[i:i+3]) for i in range(0, len(values), 3)]

        # Check that length of rgb array is correct, if yes then loop through pixels until a wrong one is found
        if len(rgb) < len(expected):
            correct = False
            msg = ['Your PPM image contains fewer pixels than expected. Check that your for loops are printing enough pixels for the specified width, and that your height is calculated correctly.']
        elif len(rgb) > len(expected):
            correct = False
            msg = ['Your PPM image contains more pixels than expected. Check that your for loops are printing exactly as many pixels as necessary for the specified width, and that your height is calculated correctly.']
        else:
            index = 0
            for pixel in rgb:
                if pixel != expected[index]:
                    correct = False
                    index = index + 1
                    msg = [f'Pixel #{index} in your PPM image is incorrect. Fix this pixel and try again.']
                    break
                index = index + 1
        
        # Pass test pass/fail status and message to test run output
        self.assertTrue(correct, msg)
        test.terminate()