<commit_before>__author__ = '<NAME>'

import unittest
import os
from PIL import Image
from lands.draw import *
from lands.draw import _biome_colors # need to be explicitly imported
from lands.world import *

class PixelCollector:

    def __init__(self, width, height):
        self.pixels = {}
        self.width = width
        self.height = height

    def set_pixel(self, x, y, color):
        if len(color) == 3:
            color = color + (255,)
        self.pixels[(x, y)] = color

    def __getitem__(self, item):
        return self.pixels[item]

class TestDraw(unittest.TestCase):

    def setUp(self):
        tests_dir = os.path.dirname(os.path.realpath(__file__))
        self.tests_data_dir = os.path.abspath(os.path.join(tests_dir, './data'))
        self.tests_blessed_images_dir = os.path.abspath(os.path.join(tests_dir, './blessed_images'))


    def test_biome_colors(self):
        self.assertEqual(Biome.all_names(), _biome_colors.keys().sort())

    def _assert_is_valid_color(self, color, color_name):
        r, g, b = color
        self.assertTrue(0.0 <= r <= 1.0, "red component %s is not in [0,1]: %f" % (color_name, r))
        self.assertTrue(0.0 <= g <= 1.0, "green component %s is not in [0,1]: %f" % (color_name, g))
        self.assertTrue(0.0 <= b <= 1.0, "blue component %s is not in [0,1]: %f" % (color_name, b))

    def _assert_img_equal(self, blessed_image_name, drawn_image):
        blessed_img = Image.open("%s/%s.png" % (self.tests_blessed_images_dir, blessed_image_name))
        blessed_img_pixels = blessed_img.load()

        blessed_img_width, blessed_img_height = blessed_img.size
        self.assertEqual(blessed_img_width, drawn_image.width)
        self.assertEqual(blessed_img_height, drawn_image.height)
        for y in range(blessed_img_height):
            for x in range(blessed_img_width):
                blessed_pixel = blessed_img_pixels[x, y]
                drawn_pixel = drawn_image[x, y]
                self.assertEqual(blessed_pixel, drawn_pixel,
                                 "Pixels at %i, %i are different. Blessed %s, drawn %s"
                                 % (x, y, blessed_pixel, drawn_pixel))

    def test_elevation_color(self):
        for i in range(0, 20):
            v = i / 2.0
            c = ra, ga, ba = elevation_color(v)
            delta = 0.0000001
            c_low = rb, gb, bb = elevation_color(v - delta)
            c_high = rc, gc, bc = elevation_color(v + delta)

            # we want values to be in range
            self._assert_is_valid_color(c, "color for %f" % v)
            self._assert_is_valid_color(c_low, "color for %f (low)" % (v - delta))
            self._assert_is_valid_color(c_high, "color for %f (high)" % (v + delta))

            # we look for discontinuities
            # TODO verify this
            #self.assertAlmostEqual(ra, rb, 5, "value %f, red, low, from %f to %f" % (v, ra, rb))
            #self.assertAlmostEqual(ra, rc, 5, "value %f, red, high, from %f to %f" % (v, ra, rc))
            #self.assertAlmostEqual(ga, gb, 5, "value %f, green, low, from %f to %f" % (v, ga, gb))
            #self.assertAlmostEqual(ga, gc, 5, "value %f, green, high, from %f to %f" % (v, ga, gc))
            #self.assertAlmostEqual(ba, bb, 5, "value %f, blue, low, from %f to %f" % (v, ba, bb))
            #self.assertAlmostEqual(ba, bc, 5, "value %f, blue, high, from %f to %f" % (v, ba, bc))

    def test_draw_simple_elevation(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        data = w.elevation['data']
        target = PixelCollector(w.width, w.height)
        draw_simple_elevation(data, w.width, w.height, w.sea_level(), target)
        self._assert_img_equal("simple_elevation_28070", target)

    def test_draw_elevation_shadow(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        data = w.elevation['data']
        target = PixelCollector(w.width, w.height)
        draw_elevation(w, True, target)
        self._assert_img_equal("elevation_28070_shadow", target)

    def test_draw_elevation_no_shadow(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        data = w.elevation['data']
        target = PixelCollector(w.width, w.height)
        draw_elevation(w, False, target)
        self._assert_img_equal("elevation_28070_no_shadow", target)

    def test_draw_riversmap(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_riversmap(w, target)
        self._assert_img_equal("riversmap_28070", target)

    def test_draw_grayscale_heightmap(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_grayscale_heightmap(w, target)
        self._assert_img_equal("grayscale_heightmap_28070", target)

    def test_draw_ocean(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_ocean(w.ocean, target)
        self._assert_img_equal("ocean_28070", target)

    def test_draw_precipitation(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_precipitation(w, target)
        self._assert_img_equal("precipitation_28070", target)

    def test_draw_world(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_world(w, target)
        self._assert_img_equal("world_28070", target)

    def test_draw_temperature_levels(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_temperature_levels(w, target)
        self._assert_img_equal("temperature_28070", target)

    def test_draw_biome(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_biome(w, target)
        self._assert_img_equal("biome_28070", target)

if __name__ == '__main__':
    unittest.main()
<commit_msg>style: minor<commit_after>__author__ = 'Federico Tomassetti'

import unittest
import os
from PIL import Image
from lands.draw import *
from lands.draw import _biome_colors # need to be explicitly imported
from lands.world import *


class PixelCollector:

    def __init__(self, width, height):
        self.pixels = {}
        self.width = width
        self.height = height

    def set_pixel(self, x, y, color):
        if len(color) == 3:
            color = color + (255,)
        self.pixels[(x, y)] = color

    def __getitem__(self, item):
        return self.pixels[item]


class TestDraw(unittest.TestCase):

    def setUp(self):
        tests_dir = os.path.dirname(os.path.realpath(__file__))
        self.tests_data_dir = os.path.abspath(os.path.join(tests_dir, './data'))
        self.tests_blessed_images_dir = os.path.abspath(os.path.join(tests_dir, './blessed_images'))

    def test_biome_colors(self):
        self.assertEqual(Biome.all_names(), _biome_colors.keys().sort())

    def _assert_is_valid_color(self, color, color_name):
        r, g, b = color
        self.assertTrue(0.0 <= r <= 1.0, "red component %s is not in [0,1]: %f" % (color_name, r))
        self.assertTrue(0.0 <= g <= 1.0, "green component %s is not in [0,1]: %f" % (color_name, g))
        self.assertTrue(0.0 <= b <= 1.0, "blue component %s is not in [0,1]: %f" % (color_name, b))

    def _assert_img_equal(self, blessed_image_name, drawn_image):
        blessed_img = Image.open("%s/%s.png" % (self.tests_blessed_images_dir, blessed_image_name))
        blessed_img_pixels = blessed_img.load()

        blessed_img_width, blessed_img_height = blessed_img.size
        self.assertEqual(blessed_img_width, drawn_image.width)
        self.assertEqual(blessed_img_height, drawn_image.height)
        for y in range(blessed_img_height):
            for x in range(blessed_img_width):
                blessed_pixel = blessed_img_pixels[x, y]
                drawn_pixel = drawn_image[x, y]
                self.assertEqual(blessed_pixel, drawn_pixel,
                                 "Pixels at %i, %i are different. Blessed %s, drawn %s"
                                 % (x, y, blessed_pixel, drawn_pixel))

    def test_elevation_color(self):
        for i in range(0, 20):
            v = i / 2.0
            c = ra, ga, ba = elevation_color(v)
            delta = 0.0000001
            c_low = rb, gb, bb = elevation_color(v - delta)
            c_high = rc, gc, bc = elevation_color(v + delta)

            # we want values to be in range
            self._assert_is_valid_color(c, "color for %f" % v)
            self._assert_is_valid_color(c_low, "color for %f (low)" % (v - delta))
            self._assert_is_valid_color(c_high, "color for %f (high)" % (v + delta))

            # we look for discontinuities
            # TODO verify this
            #self.assertAlmostEqual(ra, rb, 5, "value %f, red, low, from %f to %f" % (v, ra, rb))
            #self.assertAlmostEqual(ra, rc, 5, "value %f, red, high, from %f to %f" % (v, ra, rc))
            #self.assertAlmostEqual(ga, gb, 5, "value %f, green, low, from %f to %f" % (v, ga, gb))
            #self.assertAlmostEqual(ga, gc, 5, "value %f, green, high, from %f to %f" % (v, ga, gc))
            #self.assertAlmostEqual(ba, bb, 5, "value %f, blue, low, from %f to %f" % (v, ba, bb))
            #self.assertAlmostEqual(ba, bc, 5, "value %f, blue, high, from %f to %f" % (v, ba, bc))

    def test_draw_simple_elevation(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        data = w.elevation['data']
        target = PixelCollector(w.width, w.height)
        draw_simple_elevation(data, w.width, w.height, w.sea_level(), target)
        self._assert_img_equal("simple_elevation_28070", target)

    def test_draw_elevation_shadow(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        data = w.elevation['data']
        target = PixelCollector(w.width, w.height)
        draw_elevation(w, True, target)
        self._assert_img_equal("elevation_28070_shadow", target)

    def test_draw_elevation_no_shadow(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        data = w.elevation['data']
        target = PixelCollector(w.width, w.height)
        draw_elevation(w, False, target)
        self._assert_img_equal("elevation_28070_no_shadow", target)

    def test_draw_riversmap(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_riversmap(w, target)
        self._assert_img_equal("riversmap_28070", target)

    def test_draw_grayscale_heightmap(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_grayscale_heightmap(w, target)
        self._assert_img_equal("grayscale_heightmap_28070", target)

    def test_draw_ocean(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_ocean(w.ocean, target)
        self._assert_img_equal("ocean_28070", target)

    def test_draw_precipitation(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_precipitation(w, target)
        self._assert_img_equal("precipitation_28070", target)

    def test_draw_world(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_world(w, target)
        self._assert_img_equal("world_28070", target)

    def test_draw_temperature_levels(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_temperature_levels(w, target)
        self._assert_img_equal("temperature_28070", target)

    def test_draw_biome(self):
        w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
        target = PixelCollector(w.width, w.height)
        draw_biome(w, target)
        self._assert_img_equal("biome_28070", target)

if __name__ == '__main__':
    unittest.main()

{'fraction_non_alphanumeric': 0.12425376104433654, 'fraction_numerical': 0.022446867786356763, 'mean_word_length': 2.8328248932275777, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1311980', 'n_tokens_mistral': 4531, 'n_tokens_neox': 4253, 'n_words': 894}