def test(shape, blockshape):

    g = Graph()
    opLabel = OpSparseLabelArray(g)
    opLabelBlocked = OpBlockedSparseLabelArray(g)

    opLabel.inputs["shape"].setValue(shape[:-1] + (1,))
    opLabelBlocked.inputs["shape"].setValue(shape[:-1] + (1,))

    opLabelBlocked.inputs["blockShape"].setValue(blockshape)

    opLabel.inputs["eraser"].setValue(100)
    opLabelBlocked.inputs["eraser"].setValue(100)

    niter = 100

    for i in range(niter):

        value = numpy.random.randint(1, 10)
        key = randomKey(shape[:-1])
        # key = (slice(0, 1, None), slice(4, 39, None), 0)
        # key = (slice(25, 49, None), slice(19, 50, None), slice(37, 50, None), 0)
        start, stop = sliceToRoi(key, shape)
        diff = stop - start
        valueshape = diff[:-1]
        valuearray = numpy.zeros(tuple(valueshape), dtype=numpy.uint8)
        valuearray[:] = value
        print i, key, valuearray.shape

        opLabel.setInSlot(opLabel.inputs["Input"], key, valuearray)
        opLabelBlocked.setInSlot(opLabelBlocked.inputs["Input"], key, valuearray)
        out = opLabel.outputs["Output"][:].allocate().wait()
        # print "first done"
        outblocked = opLabelBlocked.outputs["Output"][:].allocate().wait()
        # print "second done"
        assert_array_equal(out, outblocked)
        # print out
        # print outblocked

    nz1 = opLabel.outputs["nonzeroValues"][0].allocate().wait()

    nz2 = opLabelBlocked.outputs["nonzeroValues"][0].allocate().wait()

    for nz in nz1[0]:
        assert nz in nz2[0], "%r value not in blocked set" % nz

    for nz in nz2[0]:
        assert nz in nz1[0], "%r value not in non-blocked array" % nz

    print "done!"
def veryRandomTest(shape, blockshape):
    g = Graph()
    opLabel = OpSparseLabelArray(g)
    opLabelBlocked = OpBlockedSparseLabelArray(g)

    opLabel.inputs["shape"].setValue(shape[:-1] + (1,))
    opLabelBlocked.inputs["shape"].setValue(shape[:-1] + (1,))

    opLabelBlocked.inputs["blockShape"].setValue(blockshape)

    opLabel.inputs["eraser"].setValue(100)
    opLabelBlocked.inputs["eraser"].setValue(100)
    niter = 100

    for i in range(niter):

        value = numpy.random.randint(1, 10)
        key = randomKey(shape)
        # key = (slice(1, 41, None), slice(27, 50, None), slice(12, 50, None), 0)
        # key = (slice(7, 20, None), slice(7, 50, None), slice(35, 50, None), 0)
        start, stop = sliceToRoi(key, shape)
        diff = stop - start
        valueshape = diff[:-1]
        valuearray = numpy.zeros(tuple(valueshape), dtype=numpy.uint8)
        valuearray[:] = value

        opLabel.setInSlot(opLabel.inputs["Input"], key, valuearray)
        opLabelBlocked.setInSlot(opLabelBlocked.inputs["Input"], key, valuearray)

        key2 = randomKey(shape)
        # key2 = (slice(37, 49, None), slice(38, 50, None), slice(28, 50, None), 0)
        # key2 = (slice(7, 21, None), slice(21, 50, None), slice(10, 50, None))
        print i, key, key2
        out = opLabel.outputs["Output"][key2].allocate().wait()
        # print "first done"
        outblocked = opLabelBlocked.outputs["Output"][key2].allocate().wait()
        # print "second done"
        assert_array_equal(out, outblocked)