github.com/Kintar/etxt@v0.0.0-20221224033739-2fc69f000137/cache_test.go (about)

     1  //go:build gtxt
     2  
     3  package etxt
     4  
     5  import "image"
     6  import "testing"
     7  
     8  func TestDefaultCacheConsistency(t *testing.T) {
     9  	// this test consists in writing some text without caching
    10  	// while changing text positioning and fonts, and then doing
    11  	// the same with caching to ensure that the results are the
    12  	// same
    13  	targetNoCache := image.NewRGBA(image.Rect(0, 0, 256, 128))
    14  	targetYesCache := image.NewRGBA(image.Rect(0, 0, 256, 128))
    15  	cache := NewDefaultCache(8 * 1024 * 1024) // 8MiB cache
    16  	cacheHandler := cache.NewHandler()
    17  	renderer := NewStdRenderer()
    18  	renderer.SetAlign(YCenter, XCenter)
    19  
    20  	var drawProc = func() {
    21  		y := 16
    22  		renderer.SetFont(testFont)
    23  		renderer.Draw("education", 128, y)
    24  		y += 24
    25  		renderer.SetFont(testFont2)
    26  		renderer.Draw("failure", 128, y)
    27  		y += 24
    28  		renderer.SetFont(testFont)
    29  		renderer.Draw("programming", 128, y)
    30  		y += 24
    31  		renderer.SetFont(testFont2)
    32  		renderer.Draw("disaster", 128, y)
    33  	}
    34  
    35  	// first test config
    36  	renderer.SetSizePx(16)
    37  	renderer.SetQuantizerStep(64, 64)
    38  
    39  	renderer.SetCacheHandler(nil)
    40  	renderer.SetTarget(targetNoCache)
    41  	drawProc()
    42  	renderer.SetCacheHandler(cacheHandler)
    43  	renderer.SetTarget(targetYesCache)
    44  	drawProc()
    45  	compareDrawResults("FullQuant16", t, targetNoCache, targetYesCache)
    46  
    47  	// second test config
    48  	renderer.SetSizePx(12)
    49  	renderer.SetQuantizerStep(1, 64)
    50  
    51  	renderer.SetCacheHandler(nil)
    52  	renderer.SetTarget(targetNoCache)
    53  	drawProc()
    54  	renderer.SetCacheHandler(cacheHandler)
    55  	renderer.SetTarget(targetYesCache)
    56  	drawProc()
    57  	compareDrawResults("VertQuant12", t, targetNoCache, targetYesCache)
    58  
    59  	// third test config
    60  	renderer.SetSizePx(17)
    61  	renderer.SetQuantizerStep(1, 1)
    62  
    63  	renderer.SetCacheHandler(nil)
    64  	renderer.SetTarget(targetNoCache)
    65  	drawProc()
    66  	renderer.SetCacheHandler(cacheHandler)
    67  	renderer.SetTarget(targetYesCache)
    68  	drawProc()
    69  	compareDrawResults("NoQuant17", t, targetNoCache, targetYesCache)
    70  
    71  	// ...
    72  }
    73  
    74  func compareDrawResults(testKey string, t *testing.T, targetNoCache, targetYesCache *image.RGBA) {
    75  	const ShowMeVisually = false
    76  	for i := 0; i < len(targetNoCache.Pix); i++ {
    77  		a := targetNoCache.Pix[i]
    78  		b := targetYesCache.Pix[i]
    79  		if a != b {
    80  			if ShowMeVisually {
    81  				debugExport("test_failure_no_cache.png", targetNoCache)
    82  				debugExport("test_failure_yes_cache.png", targetYesCache)
    83  			}
    84  			t.Fatalf("Cache consistency test '%s' failed, i = %d has values %d vs %d", testKey, i, a, b)
    85  		}
    86  	}
    87  }