diff options
author | Steve Chaplin <> | 2012-04-25 12:25:28 +0800 |
---|---|---|
committer | Steve Chaplin <> | 2012-04-25 12:25:28 +0800 |
commit | beb3e1cb641ef4f5461b0e360d7d8d3c9fbcf4aa (patch) | |
tree | f73c9ec164effd43eed5793be08e3ea17392b504 | |
parent | a2d54c73e2d50159fef593d112e1c1dccb8b4222 (diff) | |
download | pycairo-beb3e1cb641ef4f5461b0e360d7d8d3c9fbcf4aa.tar.gz |
Update tests.
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | py3cairo.pc | 8 | ||||
-rwxr-xr-x | setup.py | 14 | ||||
-rwxr-xr-x | test/isurface_create_for_data.py | 67 | ||||
-rwxr-xr-x | test/isurface_create_for_data1.py | 32 | ||||
-rwxr-xr-x | test/isurface_create_for_data2.py | 30 | ||||
-rwxr-xr-x | test/isurface_get_data.py | 12 | ||||
-rw-r--r-- | test/ref_count_test.py | 74 |
8 files changed, 156 insertions, 83 deletions
@@ -15,4 +15,6 @@ waf.* build build_directory +py3cairo.pc + src/config.h diff --git a/py3cairo.pc b/py3cairo.pc deleted file mode 100644 index aea7820..0000000 --- a/py3cairo.pc +++ /dev/null @@ -1,8 +0,0 @@ -prefix=/a - -Name: Pycairo -Description: Python 3 bindings for cairo -Version: 1.10.0 -Requires: cairo -Cflags: -I${prefix}/include/pycairo -Libs: @@ -9,8 +9,8 @@ import os import subprocess import sys -pycairo_version = '1.10.0' -cairo_version_required = '1.10.0' +pycairo_version = '1.10.1' +cairo_version_required = '1.10.2' python_version_required = (3,0) pkgconfig_file = 'py3cairo.pc' config_file = 'src/config.h' @@ -24,13 +24,13 @@ def call(command): return pipe def pkg_config_version_check(pkg, version): - pipe = call('pkg-config --print-errors --exists "%s >= %s"' % - (pkg, version)) + check = '%s >= %s' % (pkg, version) + pipe = call("pkg-config --print-errors --exists '%s'" % (check,)) if pipe.returncode == 0: - print('%s >= %s detected' % (pkg, version)) + print(check, ' Successful') else: - print(pipe.stderr.read()) - raise SystemExit('Error: %s >= %s not found' % (pkg, version)) + print(check, ' Failed') + raise SystemExit(pipe.stderr.read().decode()) def pkg_config_parse(opt, pkg): pipe = call("pkg-config %s %s" % (opt, pkg)) diff --git a/test/isurface_create_for_data.py b/test/isurface_create_for_data.py new file mode 100755 index 0000000..22c0673 --- /dev/null +++ b/test/isurface_create_for_data.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +"""test cairo.ImageSurface.create_for_data() +""" + +import array +import tempfile + +import cairo + +if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS): + raise SystemExit ('cairo was not compiled with ImageSurface and PNG support') + + +def test_python_array(): + h, fileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png') + width, height = 255, 255 + data = array.array('B', [0] * width * height * 4) + + for y in range(height): + for x in range(width): + offset = (x + (y * width)) * 4 + alpha = y + + # cairo.FORMAT_ARGB32 uses pre-multiplied alpha + data[offset+0] = int(x * alpha/255.0) # B + data[offset+1] = int(y * alpha/255.0) # G + data[offset+2] = 0 # R + data[offset+3] = alpha # A + + surface = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32, + width, height) + ctx = cairo.Context(surface) + surface.write_to_png(fileName) + print("see %s output file" % fileName) + + +def test_numpy_array(): + "create_for_data() using numpy" + try: + import numpy + except: + print("numpy not installed") + return + + h, fileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png') + width, height = 255, 255 + data = numpy.ndarray (shape=(height,width,4), dtype=numpy.uint8) + + for x in range(width): + for y in range(height): + alpha = y + + # cairo.FORMAT_ARGB32 uses pre-multiplied alpha + data[y][x][0] = int(x * alpha/255.0) # B + data[y][x][1] = int(y * alpha/255.0) # G + data[y][x][2] = 0 # R + data[y][x][3] = alpha # A + + surface = cairo.ImageSurface.create_for_data (data, cairo.FORMAT_ARGB32, + width, height) + ctx = cairo.Context(surface) + surface.write_to_png(fileName) + print("see %s output file" % fileName) + + +test_python_array() +test_numpy_array() diff --git a/test/isurface_create_for_data1.py b/test/isurface_create_for_data1.py deleted file mode 100755 index 7ad5496..0000000 --- a/test/isurface_create_for_data1.py +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env python -"""test cairo.ImageSurface.create_for_data() with a Python array -""" - -import array -import tempfile - -import cairo - -if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS): - raise SystemExit ('cairo was not compiled with ImageSurface and PNG support') - -h, fileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png') -width, height = 255, 255 -data = array.array('B', [0] * width * height * 4) - -for y in range(height): - for x in range(width): - offset = (x + (y * width)) * 4 - alpha = y - - # cairo.FORMAT_ARGB32 uses pre-multiplied alpha - data[offset+0] = int(x * alpha/255.0) # B - data[offset+1] = int(y * alpha/255.0) # G - data[offset+2] = 0 # R - data[offset+3] = alpha # A - -surface = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32, - width, height) -ctx = cairo.Context(surface) -surface.write_to_png(fileName) -print("see %s output file" % fileName) diff --git a/test/isurface_create_for_data2.py b/test/isurface_create_for_data2.py deleted file mode 100755 index c4f290b..0000000 --- a/test/isurface_create_for_data2.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python -"""test cairo.ImageSurface.create_for_data() with a numpy array -""" -import tempfile - -import cairo -import numpy - -if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS): - raise SystemExit ('cairo was not compiled with ImageSurface and PNG support') - -h, fileName = tempfile.mkstemp(prefix='pycairo_', suffix='.png') -width, height = 255, 255 -data = numpy.ndarray (shape=(height,width,4), dtype=numpy.uint8) - -for x in range(width): - for y in range(height): - alpha = y - - # cairo.FORMAT_ARGB32 uses pre-multiplied alpha - data[y][x][0] = int(x * alpha/255.0) # B - data[y][x][1] = int(y * alpha/255.0) # G - data[y][x][2] = 0 # R - data[y][x][3] = alpha # A - -surface = cairo.ImageSurface.create_for_data (data, cairo.FORMAT_ARGB32, - width, height) -ctx = cairo.Context(surface) -surface.write_to_png(fileName) -print "see %s output file" % fileName diff --git a/test/isurface_get_data.py b/test/isurface_get_data.py index 6875042..af114c9 100755 --- a/test/isurface_get_data.py +++ b/test/isurface_get_data.py @@ -9,11 +9,11 @@ import cairo if not (cairo.HAS_IMAGE_SURFACE and cairo.HAS_PNG_FUNCTIONS): raise SystemExit ('cairo was not compiled with ImageSurface and PNG support') -w, h = 128, 128 +width, height = 128, 128 -def create_surface(): +def create_surface(cformat, w, h): "create black triangle on white background" - surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) + surface = cairo.ImageSurface(cformat, w, h) ctx = cairo.Context(surface) ctx.set_source_rgb(1, 1, 1) # white @@ -34,13 +34,13 @@ def create_surface(): def test_python_buffer(): "get_data() and modify data using Python" - surface = create_surface() + surface = create_surface(cairo.FORMAT_ARGB32, width, height) _, f1 = tempfile.mkstemp(prefix='pycairo_', suffix='.png') surface.write_to_png(f1) buf = surface.get_data() stride = surface.get_stride() - for i in range(h): + for i in range(height): offset = i * stride + 120 buf[offset] = b'\xFF' buf[offset + 1] = b'\x00' @@ -63,7 +63,7 @@ def test_numpy_and_python_buffer(): print("numpy not installed") return - surface = create_surface() + surface = create_surface(cairo.FORMAT_ARGB32, width, height) _, f1 = tempfile.mkstemp(prefix='pycairo_', suffix='.png') surface.write_to_png(f1) diff --git a/test/ref_count_test.py b/test/ref_count_test.py new file mode 100644 index 0000000..a16be75 --- /dev/null +++ b/test/ref_count_test.py @@ -0,0 +1,74 @@ +'''test for reference counting problems. + +If a Python object is garbage collected while another object is using its +data, you will get a segmentation fault. +''' + +import array +import gc +import tempfile as tfi + +import cairo +import py.test as test + +width, height = 256, 256 + +def draw(ctx, width, height): + "example draw code" + ctx.scale(width/1.0, height/1.0) + + pat = cairo.LinearGradient(0.0, 0.0, 0.0, 1.0) + pat.add_color_stop_rgba(1, 0, 0, 0, 1) + pat.add_color_stop_rgba(0, 1, 1, 1, 1) + ctx.rectangle(0,0,1,1) + ctx.set_source(pat) + ctx.fill() + + +def test_create_for_stream(): + def run_test(surface_method, suffix): + _, fo = tfi.mkstemp(prefix='pycairo_', suffix=suffix) + surface = surface_method(fo, width, height) + ctx = cairo.Context(surface) + + del fo # test that 'fo' is referenced to keep it alive + gc.collect() + + draw(ctx, width, height) + ctx.show_page() + surface.finish() + + if cairo.HAS_PDF_SURFACE: + run_test(cairo.PDFSurface, '.pdf') + if cairo.HAS_PS_SURFACE: + run_test(cairo.PSSurface, '.ps') + if cairo.HAS_SVG_SURFACE: + run_test(cairo.SVGSurface, '.svg') + + +def test_get_data(): + surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) + + memView = surface.get_data() + + del surface # test that 'surface' is referenced to keep it alive + gc.collect() + + memView[0] = b'\xFF' + data = memView.tobytes() + + +def test_create_for_data(): + data = array.array('B', [0] * width * height * 4) + + surface = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32, + width, height) + ctx = cairo.Context(surface) + + del data # test that 'data' is referenced to keep it alive + gc.collect() + + draw(ctx, width, height) + + _, fo = tfi.mkstemp(prefix='pycairo_', suffix='.png') + surface.write_to_png(fo) |