diff --git a/CHANGES.rst b/CHANGES.rst
index def94ac..c7bbf74 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -5,7 +5,8 @@
 4.4.1 (unreleased)
 ==================
 
-- Nothing changed yet.
+- Fix ``zope.index.text.ricecode.decode_deltas(..., [])``.  See
+  `issue 22 <https://github.com/zopefoundation/zope.index/issues/22>`_.
 
 
 4.4.0 (2018-10-05)
diff --git a/src/zope/index/text/ricecode.py b/src/zope/index/text/ricecode.py
index 0397606..b0b2b8a 100644
--- a/src/zope/index/text/ricecode.py
+++ b/src/zope/index/text/ricecode.py
@@ -182,8 +182,10 @@ def encode_deltas(l):
     return l[0], deltas
 
 def decode_deltas(start, enc_deltas):
-    deltas = enc_deltas.tolist()
     l = [start]
+    if not enc_deltas:
+        return l
+    deltas = enc_deltas.tolist()
     for i in range(1, len(deltas)):
         l.append(l[i-1] + deltas[i])
     l.append(l[-1] + deltas[-1])

Tests can randomly fail
`zope.index.text.ricecode.encode_deltas(a_list)` returns a tuple `(start, deltas)`.  Usually `deltas` is an instance of `zope.index.text.ricecode.RiceCode`, but when `len(a_list) == 1)`, `deltas` is a simple `[]`.

This is an awkward API that causes problems: the inverse `decode_deltas(start, deltas)` method requires `deltas` to be a `RiceCode` and fails if you pass in an empty list.

This case can be seen in the zope.index's own testsuite, where `test_random` can fail non-deterministically.

Originally discovered in  
https://github.com/zopefoundation/zope.index/pull/21#issuecomment-443264249
diff --git a/src/zope/index/text/tests/test_ricecode.py b/src/zope/index/text/tests/test_ricecode.py
index 1f8356e..9c30846 100644
--- a/src/zope/index/text/tests/test_ricecode.py
+++ b/src/zope/index/text/tests/test_ricecode.py
@@ -15,11 +15,11 @@ class TestRiceCode(unittest.TestCase):
 
     def test_random(self):
         import random
-        for size in [10, 20, 50, 100, 200]:
+        for size in [1, 10, 20, 50, 100, 200]:
             l = [random.randint(1, size) for i in range(50)]
             c = encode(random.randint(1, 16), l)
             self.assertEqual(c.tolist(), l)
-        for size in [10, 20, 50, 100, 200]:
+        for size in [1, 10, 20, 50, 100, 200]:
             l = list(range(random.randint(1, size), size + random.randint(1, size)))
             l_0, deltas = encode_deltas(l)
             l2 = decode_deltas(l_0, deltas)
@@ -29,6 +29,10 @@ class TestRiceCode(unittest.TestCase):
         self.assertEqual(('foo', []),
                          encode_deltas(['foo']))
 
+    def test_decode_deltas_one_element(self):
+        self.assertEqual(['foo'],
+                         decode_deltas('foo', []))
+
     def test_pickle_efficiency(self):
         # This is random data so it's hard to say what wins
         # and what loses.

pip install -e .[test]
parse_log_pytest
pytest
coverage pytest
apt-get update apt-get install -y gcc
3.6
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs @ file:///opt/conda/conda-bld/attrs_1642510447205/work
BTrees==4.11.3
certifi==2021.5.30
cffi==1.15.1
coverage==6.2
importlib-metadata @ file:///tmp/build/80754af9/importlib-metadata_1631916693255/work
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
more-itertools @ file:///tmp/build/80754af9/more-itertools_1637733554872/work
packaging @ file:///tmp/build/80754af9/packaging_1637314298585/work
persistent==4.9.3
pluggy @ file:///tmp/build/80754af9/pluggy_1615976315926/work
py @ file:///opt/conda/conda-bld/py_1644396412707/work
pycparser==2.21
pyparsing @ file:///tmp/build/80754af9/pyparsing_1635766073266/work
pytest==6.2.4
six==1.17.0
toml @ file:///tmp/build/80754af9/toml_1616166611790/work
typing_extensions @ file:///opt/conda/conda-bld/typing_extensions_1647553014482/work
zipp @ file:///tmp/build/80754af9/zipp_1633618647012/work
zope.exceptions==4.6
-e git+https://github.com/zopefoundation/zope.index.git@740285a2f27ed23549e09603e5cdb44ac0019671#egg=zope.index
zope.interface==5.5.2
zope.testrunner==5.6

name: zope.index
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=21.4.0=pyhd3eb1b0_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - importlib-metadata=4.8.1=py36h06a4308_0
  - importlib_metadata=4.8.1=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - more-itertools=8.12.0=pyhd3eb1b0_0
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=21.3=pyhd3eb1b0_0
  - pip=21.2.2=py36h06a4308_0
  - pluggy=0.13.1=py36h06a4308_0
  - py=1.11.0=pyhd3eb1b0_0
  - pyparsing=3.0.4=pyhd3eb1b0_0
  - pytest=6.2.4=py36h06a4308_2
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - toml=0.10.2=pyhd3eb1b0_0
  - typing_extensions=4.1.1=pyh06a4308_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.6.0=pyhd3eb1b0_0
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - btrees==4.11.3
      - cffi==1.15.1
      - coverage==6.2
      - persistent==4.9.3
      - pycparser==2.21
      - six==1.17.0
      - zope-exceptions==4.6
      - zope-interface==5.5.2
      - zope-testrunner==5.6
prefix: /opt/conda/envs/zope.index

src/zope/index/text/tests/test_ricecode.py::TestRiceCode::test_decode_deltas_one_element src/zope/index/text/tests/test_ricecode.py::TestRiceCode::test_random
src/zope/index/text/tests/test_ricecode.py::TestRiceCode::test_append_invalid src/zope/index/text/tests/test_ricecode.py::TestRiceCode::test_bit_range src/zope/index/text/tests/test_ricecode.py::TestRiceCode::test_encode_deltas_one_element src/zope/index/text/tests/test_ricecode.py::TestRiceCode::test_length src/zope/index/text/tests/test_ricecode.py::TestRiceCode::test_pickle src/zope/index/text/tests/test_ricecode.py::TestRiceCode::test_pickle_efficiency
src/zope/index/text/ricecode.py CHANGES.rst
src/zope/index/text/ricecode.py CHANGES.rst