text: // Copyright (C) 2020 T. Zachary Laine
  //
  // Distributed under the Boost Software License, Version 1.0. (See
  // accompanying file LICENSE_1_0.txt or copy at
  // http://www.boost.org/LICENSE_1_0.txt)
  
  // Warning! This file is autogenerated.
  #include <boost/text/bidirectional.hpp>
  
  #include "bidi_tests.hpp"
  
  #include <gtest/gtest.h>
  
  #include <algorithm>
  
  
  std::vector<int> expected_levels;
  std::vector<int> expected_reordered_indices;
  
  TEST(bidi, bidi_572_000)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // ON RLE LRI L; 4 ('RTL') (line 286001)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2066, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE LRI EN; 4 ('RTL') (line 286002)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2066, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE LRI ES; 4 ('RTL') (line 286003)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2066, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE LRI ET; 4 ('RTL') (line 286004)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2066, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE LRI CS; 4 ('RTL') (line 286005)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2066, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE LRI NSM; 4 ('RTL') (line 286006)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2066, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE LRI ON; 4 ('RTL') (line 286007)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2066, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE FSI L; 4 ('RTL') (line 286008)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2068, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE FSI EN; 4 ('RTL') (line 286009)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2068, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE FSI ES; 4 ('RTL') (line 286010)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2068, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_001)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // ON RLE FSI ET; 4 ('RTL') (line 286011)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2068, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE FSI CS; 4 ('RTL') (line 286012)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2068, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE FSI NSM; 4 ('RTL') (line 286013)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2068, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE FSI ON; 4 ('RTL') (line 286014)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2068, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE PDI L; 4 ('RTL') (line 286015)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2069, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE PDI EN; 4 ('RTL') (line 286016)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2069, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE PDI AN; 4 ('RTL') (line 286017)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2069, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO LRI L; 4 ('RTL') (line 286018)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2066, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO LRI EN; 4 ('RTL') (line 286019)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2066, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO LRI ES; 4 ('RTL') (line 286020)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2066, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_002)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // ON RLO LRI ET; 4 ('RTL') (line 286021)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2066, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO LRI CS; 4 ('RTL') (line 286022)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2066, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO LRI NSM; 4 ('RTL') (line 286023)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2066, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO LRI ON; 4 ('RTL') (line 286024)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2066, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO FSI L; 4 ('RTL') (line 286025)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2068, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO FSI EN; 4 ('RTL') (line 286026)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2068, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO FSI ES; 4 ('RTL') (line 286027)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2068, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO FSI ET; 4 ('RTL') (line 286028)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2068, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO FSI CS; 4 ('RTL') (line 286029)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2068, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO FSI NSM; 4 ('RTL') (line 286030)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2068, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_003)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // ON RLO FSI ON; 4 ('RTL') (line 286031)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2068, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE R L; 4 ('RTL') (line 286032)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x05BE, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE R EN; 4 ('RTL') (line 286033)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x05BE, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE R AN; 4 ('RTL') (line 286034)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x05BE, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE AL L; 4 ('RTL') (line 286035)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0608, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE AL EN; 4 ('RTL') (line 286036)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0608, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE AL AN; 4 ('RTL') (line 286037)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0608, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE ES L; 4 ('RTL') (line 286038)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x002B, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE ES EN; 4 ('RTL') (line 286039)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x002B, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE ES AN; 4 ('RTL') (line 286040)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x002B, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_004)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // LRI RLE ET L; 4 ('RTL') (line 286041)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0023, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE ET AN; 4 ('RTL') (line 286042)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0023, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE CS L; 4 ('RTL') (line 286043)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x002C, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE CS EN; 4 ('RTL') (line 286044)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x002C, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE CS AN; 4 ('RTL') (line 286045)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x002C, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE NSM L; 4 ('RTL') (line 286046)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0300, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE NSM EN; 4 ('RTL') (line 286047)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0300, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE NSM AN; 4 ('RTL') (line 286048)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0300, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE WS L; 4 ('RTL') (line 286049)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0020, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE WS EN; 4 ('RTL') (line 286050)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0020, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_005)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // LRI RLE WS AN; 4 ('RTL') (line 286051)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0020, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE ON L; 4 ('RTL') (line 286052)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0021, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE ON EN; 4 ('RTL') (line 286053)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0021, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE ON AN; 4 ('RTL') (line 286054)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x0021, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE LRI L; 4 ('RTL') (line 286055)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2066, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE LRI EN; 4 ('RTL') (line 286056)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2066, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE LRI ES; 4 ('RTL') (line 286057)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2066, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE LRI ET; 4 ('RTL') (line 286058)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2066, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE LRI CS; 4 ('RTL') (line 286059)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2066, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE LRI NSM; 4 ('RTL') (line 286060)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2066, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_006)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // LRI RLE LRI ON; 4 ('RTL') (line 286061)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2066, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE FSI L; 4 ('RTL') (line 286062)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2068, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE FSI EN; 4 ('RTL') (line 286063)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2068, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE FSI ES; 4 ('RTL') (line 286064)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2068, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE FSI ET; 4 ('RTL') (line 286065)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2068, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE FSI CS; 4 ('RTL') (line 286066)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2068, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE FSI NSM; 4 ('RTL') (line 286067)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2068, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE FSI ON; 4 ('RTL') (line 286068)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2068, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO LRI L; 4 ('RTL') (line 286069)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2066, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO LRI EN; 4 ('RTL') (line 286070)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2066, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_007)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // LRI RLO LRI ES; 4 ('RTL') (line 286071)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2066, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO LRI ET; 4 ('RTL') (line 286072)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2066, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO LRI CS; 4 ('RTL') (line 286073)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2066, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO LRI NSM; 4 ('RTL') (line 286074)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2066, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO LRI ON; 4 ('RTL') (line 286075)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2066, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO FSI L; 4 ('RTL') (line 286076)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2068, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO FSI EN; 4 ('RTL') (line 286077)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2068, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO FSI ES; 4 ('RTL') (line 286078)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2068, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO FSI ET; 4 ('RTL') (line 286079)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2068, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO FSI CS; 4 ('RTL') (line 286080)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2068, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_008)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // LRI RLO FSI NSM; 4 ('RTL') (line 286081)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2068, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO FSI ON; 4 ('RTL') (line 286082)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2068, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI PDF R EN; 4 ('RTL') (line 286083)
          std::vector<uint32_t> const cps = { 0x2066, 0x202C, 0x05BE, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI PDF R AN; 4 ('RTL') (line 286084)
          std::vector<uint32_t> const cps = { 0x2066, 0x202C, 0x05BE, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI PDF AL EN; 4 ('RTL') (line 286085)
          std::vector<uint32_t> const cps = { 0x2066, 0x202C, 0x0608, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI PDF AL AN; 4 ('RTL') (line 286086)
          std::vector<uint32_t> const cps = { 0x2066, 0x202C, 0x0608, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI BN R EN; 4 ('RTL') (line 286087)
          std::vector<uint32_t> const cps = { 0x2066, 0x00AD, 0x05BE, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI BN R AN; 4 ('RTL') (line 286088)
          std::vector<uint32_t> const cps = { 0x2066, 0x00AD, 0x05BE, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI BN AL EN; 4 ('RTL') (line 286089)
          std::vector<uint32_t> const cps = { 0x2066, 0x00AD, 0x0608, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI BN AL AN; 4 ('RTL') (line 286090)
          std::vector<uint32_t> const cps = { 0x2066, 0x00AD, 0x0608, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_009)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // RLI PDF R L; 4 ('RTL') (line 286091)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x05BE, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF R EN; 4 ('RTL') (line 286092)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x05BE, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF R AN; 4 ('RTL') (line 286093)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x05BE, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF AL L; 4 ('RTL') (line 286094)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0608, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF AL EN; 4 ('RTL') (line 286095)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0608, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF AL AN; 4 ('RTL') (line 286096)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0608, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF ES L; 4 ('RTL') (line 286097)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x002B, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF ES EN; 4 ('RTL') (line 286098)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x002B, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF ES AN; 4 ('RTL') (line 286099)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x002B, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF ET L; 4 ('RTL') (line 286100)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0023, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_010)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // RLI PDF ET AN; 4 ('RTL') (line 286101)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0023, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF CS L; 4 ('RTL') (line 286102)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x002C, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF CS EN; 4 ('RTL') (line 286103)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x002C, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF CS AN; 4 ('RTL') (line 286104)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x002C, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF NSM L; 4 ('RTL') (line 286105)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0300, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF NSM EN; 4 ('RTL') (line 286106)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0300, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF NSM AN; 4 ('RTL') (line 286107)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0300, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF WS L; 4 ('RTL') (line 286108)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0020, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF WS EN; 4 ('RTL') (line 286109)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0020, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF WS AN; 4 ('RTL') (line 286110)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0020, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_011)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // RLI PDF ON L; 4 ('RTL') (line 286111)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0021, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF ON EN; 4 ('RTL') (line 286112)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0021, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF ON AN; 4 ('RTL') (line 286113)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x0021, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF LRI L; 4 ('RTL') (line 286114)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2066, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF LRI EN; 4 ('RTL') (line 286115)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2066, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF LRI ES; 4 ('RTL') (line 286116)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2066, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF LRI ET; 4 ('RTL') (line 286117)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2066, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF LRI CS; 4 ('RTL') (line 286118)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2066, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF LRI NSM; 4 ('RTL') (line 286119)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2066, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF LRI ON; 4 ('RTL') (line 286120)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2066, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_012)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // RLI PDF FSI L; 4 ('RTL') (line 286121)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2068, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF FSI EN; 4 ('RTL') (line 286122)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2068, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF FSI ES; 4 ('RTL') (line 286123)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2068, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF FSI ET; 4 ('RTL') (line 286124)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2068, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF FSI CS; 4 ('RTL') (line 286125)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2068, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF FSI NSM; 4 ('RTL') (line 286126)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2068, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI PDF FSI ON; 4 ('RTL') (line 286127)
          std::vector<uint32_t> const cps = { 0x2067, 0x202C, 0x2068, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN R L; 4 ('RTL') (line 286128)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x05BE, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN R EN; 4 ('RTL') (line 286129)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x05BE, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN R AN; 4 ('RTL') (line 286130)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x05BE, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_013)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // RLI BN AL L; 4 ('RTL') (line 286131)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0608, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN AL EN; 4 ('RTL') (line 286132)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0608, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN AL AN; 4 ('RTL') (line 286133)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0608, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN ES L; 4 ('RTL') (line 286134)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x002B, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN ES EN; 4 ('RTL') (line 286135)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x002B, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN ES AN; 4 ('RTL') (line 286136)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x002B, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN ET L; 4 ('RTL') (line 286137)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0023, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN ET AN; 4 ('RTL') (line 286138)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0023, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN CS L; 4 ('RTL') (line 286139)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x002C, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN CS EN; 4 ('RTL') (line 286140)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x002C, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_014)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // RLI BN CS AN; 4 ('RTL') (line 286141)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x002C, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN NSM L; 4 ('RTL') (line 286142)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0300, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN NSM EN; 4 ('RTL') (line 286143)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0300, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN NSM AN; 4 ('RTL') (line 286144)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0300, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN WS L; 4 ('RTL') (line 286145)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0020, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN WS EN; 4 ('RTL') (line 286146)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0020, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN WS AN; 4 ('RTL') (line 286147)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0020, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN ON L; 4 ('RTL') (line 286148)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0021, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN ON EN; 4 ('RTL') (line 286149)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0021, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN ON AN; 4 ('RTL') (line 286150)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x0021, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_015)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // RLI BN LRI L; 4 ('RTL') (line 286151)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2066, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN LRI EN; 4 ('RTL') (line 286152)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2066, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN LRI ES; 4 ('RTL') (line 286153)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2066, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN LRI ET; 4 ('RTL') (line 286154)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2066, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN LRI CS; 4 ('RTL') (line 286155)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2066, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN LRI NSM; 4 ('RTL') (line 286156)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2066, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN LRI ON; 4 ('RTL') (line 286157)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2066, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN FSI L; 4 ('RTL') (line 286158)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2068, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN FSI EN; 4 ('RTL') (line 286159)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2068, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN FSI ES; 4 ('RTL') (line 286160)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2068, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_016)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // RLI BN FSI ET; 4 ('RTL') (line 286161)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2068, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN FSI CS; 4 ('RTL') (line 286162)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2068, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN FSI NSM; 4 ('RTL') (line 286163)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2068, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // RLI BN FSI ON; 4 ('RTL') (line 286164)
          std::vector<uint32_t> const cps = { 0x2067, 0x00AD, 0x2068, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE ES L; 4 ('RTL') (line 286165)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x002B, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE ES EN; 4 ('RTL') (line 286166)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x002B, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE ES AN; 4 ('RTL') (line 286167)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x002B, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE ET L; 4 ('RTL') (line 286168)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x0023, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE ET AN; 4 ('RTL') (line 286169)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x0023, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE CS L; 4 ('RTL') (line 286170)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x002C, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_017)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // FSI RLE CS EN; 4 ('RTL') (line 286171)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x002C, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE CS AN; 4 ('RTL') (line 286172)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x002C, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE NSM L; 4 ('RTL') (line 286173)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x0300, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE NSM EN; 4 ('RTL') (line 286174)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x0300, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE NSM AN; 4 ('RTL') (line 286175)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x0300, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE WS L; 4 ('RTL') (line 286176)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x0020, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE WS EN; 4 ('RTL') (line 286177)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x0020, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE WS AN; 4 ('RTL') (line 286178)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x0020, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE ON L; 4 ('RTL') (line 286179)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x0021, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE ON EN; 4 ('RTL') (line 286180)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x0021, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_018)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // FSI RLE ON AN; 4 ('RTL') (line 286181)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x0021, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE LRI L; 4 ('RTL') (line 286182)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2066, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE LRI EN; 4 ('RTL') (line 286183)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2066, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE LRI ES; 4 ('RTL') (line 286184)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2066, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE LRI ET; 4 ('RTL') (line 286185)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2066, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE LRI CS; 4 ('RTL') (line 286186)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2066, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE LRI NSM; 4 ('RTL') (line 286187)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2066, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE LRI ON; 4 ('RTL') (line 286188)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2066, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE FSI L; 4 ('RTL') (line 286189)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2068, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE FSI EN; 4 ('RTL') (line 286190)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2068, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_019)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // FSI RLE FSI ES; 4 ('RTL') (line 286191)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2068, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE FSI ET; 4 ('RTL') (line 286192)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2068, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE FSI CS; 4 ('RTL') (line 286193)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2068, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE FSI NSM; 4 ('RTL') (line 286194)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2068, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLE FSI ON; 4 ('RTL') (line 286195)
          std::vector<uint32_t> const cps = { 0x2068, 0x202B, 0x2068, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO LRI L; 4 ('RTL') (line 286196)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2066, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO LRI EN; 4 ('RTL') (line 286197)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2066, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO LRI ES; 4 ('RTL') (line 286198)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2066, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO LRI ET; 4 ('RTL') (line 286199)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2066, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO LRI CS; 4 ('RTL') (line 286200)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2066, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_020)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // FSI RLO LRI NSM; 4 ('RTL') (line 286201)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2066, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO LRI ON; 4 ('RTL') (line 286202)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2066, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO FSI L; 4 ('RTL') (line 286203)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2068, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO FSI EN; 4 ('RTL') (line 286204)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2068, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO FSI ES; 4 ('RTL') (line 286205)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2068, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO FSI ET; 4 ('RTL') (line 286206)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2068, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO FSI CS; 4 ('RTL') (line 286207)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2068, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO FSI NSM; 4 ('RTL') (line 286208)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2068, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI RLO FSI ON; 4 ('RTL') (line 286209)
          std::vector<uint32_t> const cps = { 0x2068, 0x202E, 0x2068, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI PDF R L; 4 ('RTL') (line 286210)
          std::vector<uint32_t> const cps = { 0x2068, 0x202C, 0x05BE, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_021)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // FSI PDF R EN; 4 ('RTL') (line 286211)
          std::vector<uint32_t> const cps = { 0x2068, 0x202C, 0x05BE, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI PDF R AN; 4 ('RTL') (line 286212)
          std::vector<uint32_t> const cps = { 0x2068, 0x202C, 0x05BE, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI PDF AL L; 4 ('RTL') (line 286213)
          std::vector<uint32_t> const cps = { 0x2068, 0x202C, 0x0608, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI PDF AL EN; 4 ('RTL') (line 286214)
          std::vector<uint32_t> const cps = { 0x2068, 0x202C, 0x0608, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI PDF AL AN; 4 ('RTL') (line 286215)
          std::vector<uint32_t> const cps = { 0x2068, 0x202C, 0x0608, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI BN R L; 4 ('RTL') (line 286216)
          std::vector<uint32_t> const cps = { 0x2068, 0x00AD, 0x05BE, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI BN R EN; 4 ('RTL') (line 286217)
          std::vector<uint32_t> const cps = { 0x2068, 0x00AD, 0x05BE, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI BN R AN; 4 ('RTL') (line 286218)
          std::vector<uint32_t> const cps = { 0x2068, 0x00AD, 0x05BE, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI BN AL L; 4 ('RTL') (line 286219)
          std::vector<uint32_t> const cps = { 0x2068, 0x00AD, 0x0608, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // FSI BN AL EN; 4 ('RTL') (line 286220)
          std::vector<uint32_t> const cps = { 0x2068, 0x00AD, 0x0608, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_022)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // FSI BN AL AN; 4 ('RTL') (line 286221)
          std::vector<uint32_t> const cps = { 0x2068, 0x00AD, 0x0608, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI LRE R EN; 5 ('auto') (line 286222)
          std::vector<uint32_t> const cps = { 0x2069, 0x202A, 0x05BE, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI LRE R EN; 5 ('RTL') (line 286222)
          std::vector<uint32_t> const cps = { 0x2069, 0x202A, 0x05BE, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI LRE R AN; 5 ('auto') (line 286223)
          std::vector<uint32_t> const cps = { 0x2069, 0x202A, 0x05BE, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI LRE R AN; 5 ('RTL') (line 286223)
          std::vector<uint32_t> const cps = { 0x2069, 0x202A, 0x05BE, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI LRE AL EN; 5 ('auto') (line 286224)
          std::vector<uint32_t> const cps = { 0x2069, 0x202A, 0x0608, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI LRE AL EN; 5 ('RTL') (line 286224)
          std::vector<uint32_t> const cps = { 0x2069, 0x202A, 0x0608, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI LRE AL AN; 5 ('auto') (line 286225)
          std::vector<uint32_t> const cps = { 0x2069, 0x202A, 0x0608, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI LRE AL AN; 5 ('RTL') (line 286225)
          std::vector<uint32_t> const cps = { 0x2069, 0x202A, 0x0608, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE R L; 5 ('auto') (line 286226)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x05BE, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE R L; 5 ('RTL') (line 286226)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x05BE, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE R EN; 5 ('auto') (line 286227)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x05BE, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE R EN; 5 ('RTL') (line 286227)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x05BE, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE R AN; 5 ('auto') (line 286228)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x05BE, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE R AN; 5 ('RTL') (line 286228)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x05BE, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE AL L; 5 ('auto') (line 286229)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0608, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE AL L; 5 ('RTL') (line 286229)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0608, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE AL EN; 5 ('auto') (line 286230)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0608, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE AL EN; 5 ('RTL') (line 286230)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0608, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_023)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // PDI RLE AL AN; 5 ('auto') (line 286231)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0608, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE AL AN; 5 ('RTL') (line 286231)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0608, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE ES L; 4 ('RTL') (line 286232)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x002B, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE ES EN; 4 ('RTL') (line 286233)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x002B, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE ES AN; 4 ('RTL') (line 286234)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x002B, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE ET L; 4 ('RTL') (line 286235)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0023, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE ET AN; 4 ('RTL') (line 286236)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0023, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE CS L; 4 ('RTL') (line 286237)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x002C, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE CS EN; 4 ('RTL') (line 286238)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x002C, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE CS AN; 4 ('RTL') (line 286239)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x002C, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE NSM L; 4 ('RTL') (line 286240)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0300, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_024)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // PDI RLE NSM EN; 4 ('RTL') (line 286241)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0300, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE NSM AN; 4 ('RTL') (line 286242)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0300, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE WS L; 4 ('RTL') (line 286243)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0020, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE WS EN; 4 ('RTL') (line 286244)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0020, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE WS AN; 4 ('RTL') (line 286245)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0020, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE ON L; 4 ('RTL') (line 286246)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0021, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE ON EN; 4 ('RTL') (line 286247)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0021, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE ON AN; 4 ('RTL') (line 286248)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x0021, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE LRI L; 4 ('RTL') (line 286249)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2066, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE LRI EN; 4 ('RTL') (line 286250)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2066, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_025)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // PDI RLE LRI ES; 4 ('RTL') (line 286251)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2066, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE LRI ET; 4 ('RTL') (line 286252)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2066, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE LRI CS; 4 ('RTL') (line 286253)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2066, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE LRI NSM; 4 ('RTL') (line 286254)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2066, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE LRI ON; 4 ('RTL') (line 286255)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2066, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE FSI L; 4 ('RTL') (line 286256)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2068, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE FSI EN; 4 ('RTL') (line 286257)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2068, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE FSI ES; 4 ('RTL') (line 286258)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2068, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE FSI ET; 4 ('RTL') (line 286259)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2068, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE FSI CS; 4 ('RTL') (line 286260)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2068, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_026)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // PDI RLE FSI NSM; 4 ('RTL') (line 286261)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2068, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE FSI ON; 4 ('RTL') (line 286262)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2068, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE PDI L; 4 ('RTL') (line 286263)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2069, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE PDI EN; 4 ('RTL') (line 286264)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2069, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLE PDI AN; 4 ('RTL') (line 286265)
          std::vector<uint32_t> const cps = { 0x2069, 0x202B, 0x2069, 0x0660 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO LRI L; 4 ('RTL') (line 286266)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2066, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO LRI EN; 4 ('RTL') (line 286267)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2066, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO LRI ES; 4 ('RTL') (line 286268)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2066, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO LRI ET; 4 ('RTL') (line 286269)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2066, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO LRI CS; 4 ('RTL') (line 286270)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2066, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_027)
  {
  
      expected_levels = { 1, -1, 3, 4 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // PDI RLO LRI NSM; 4 ('RTL') (line 286271)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2066, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO LRI ON; 4 ('RTL') (line 286272)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2066, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO FSI L; 4 ('RTL') (line 286273)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2068, 0x0041 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO FSI EN; 4 ('RTL') (line 286274)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2068, 0x0030 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO FSI ES; 4 ('RTL') (line 286275)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2068, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO FSI ET; 4 ('RTL') (line 286276)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2068, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO FSI CS; 4 ('RTL') (line 286277)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2068, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO FSI NSM; 4 ('RTL') (line 286278)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2068, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // PDI RLO FSI ON; 4 ('RTL') (line 286279)
          std::vector<uint32_t> const cps = { 0x2069, 0x202E, 0x2068, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // R RLE LRI R; 5 ('auto') (line 286285)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE LRI R; 5 ('RTL') (line 286285)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_028)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // R RLE LRI AL; 5 ('auto') (line 286286)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE LRI AL; 5 ('RTL') (line 286286)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI R; 5 ('auto') (line 286287)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI R; 5 ('RTL') (line 286287)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI AL; 5 ('auto') (line 286288)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI AL; 5 ('RTL') (line 286288)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI ES; 5 ('auto') (line 286289)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI ES; 5 ('RTL') (line 286289)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI ET; 5 ('auto') (line 286290)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI ET; 5 ('RTL') (line 286290)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI CS; 5 ('auto') (line 286291)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI CS; 5 ('RTL') (line 286291)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI NSM; 5 ('auto') (line 286292)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI NSM; 5 ('RTL') (line 286292)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI ON; 5 ('auto') (line 286293)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE RLI ON; 5 ('RTL') (line 286293)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE FSI R; 5 ('auto') (line 286294)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE FSI R; 5 ('RTL') (line 286294)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE FSI AL; 5 ('auto') (line 286295)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLE FSI AL; 5 ('RTL') (line 286295)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202B, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_029)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // R RLO LRI R; 5 ('auto') (line 286296)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO LRI R; 5 ('RTL') (line 286296)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO LRI AL; 5 ('auto') (line 286297)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO LRI AL; 5 ('RTL') (line 286297)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI R; 5 ('auto') (line 286298)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI R; 5 ('RTL') (line 286298)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI AL; 5 ('auto') (line 286299)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI AL; 5 ('RTL') (line 286299)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI ES; 5 ('auto') (line 286300)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI ES; 5 ('RTL') (line 286300)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI ET; 5 ('auto') (line 286301)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI ET; 5 ('RTL') (line 286301)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI CS; 5 ('auto') (line 286302)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI CS; 5 ('RTL') (line 286302)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI NSM; 5 ('auto') (line 286303)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI NSM; 5 ('RTL') (line 286303)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI ON; 5 ('auto') (line 286304)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO RLI ON; 5 ('RTL') (line 286304)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO FSI R; 5 ('auto') (line 286305)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO FSI R; 5 ('RTL') (line 286305)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_030)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // R RLO FSI AL; 5 ('auto') (line 286306)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // R RLO FSI AL; 5 ('RTL') (line 286306)
          std::vector<uint32_t> const cps = { 0x05BE, 0x202E, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE LRI R; 5 ('auto') (line 286307)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE LRI R; 5 ('RTL') (line 286307)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE LRI AL; 5 ('auto') (line 286308)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE LRI AL; 5 ('RTL') (line 286308)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI R; 5 ('auto') (line 286309)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI R; 5 ('RTL') (line 286309)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI AL; 5 ('auto') (line 286310)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI AL; 5 ('RTL') (line 286310)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI ES; 5 ('auto') (line 286311)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI ES; 5 ('RTL') (line 286311)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI ET; 5 ('auto') (line 286312)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI ET; 5 ('RTL') (line 286312)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI CS; 5 ('auto') (line 286313)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI CS; 5 ('RTL') (line 286313)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI NSM; 5 ('auto') (line 286314)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI NSM; 5 ('RTL') (line 286314)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI ON; 5 ('auto') (line 286315)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE RLI ON; 5 ('RTL') (line 286315)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_031)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // AL RLE FSI R; 5 ('auto') (line 286316)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE FSI R; 5 ('RTL') (line 286316)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE FSI AL; 5 ('auto') (line 286317)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLE FSI AL; 5 ('RTL') (line 286317)
          std::vector<uint32_t> const cps = { 0x0608, 0x202B, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO LRI R; 5 ('auto') (line 286318)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO LRI R; 5 ('RTL') (line 286318)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO LRI AL; 5 ('auto') (line 286319)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO LRI AL; 5 ('RTL') (line 286319)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI R; 5 ('auto') (line 286320)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI R; 5 ('RTL') (line 286320)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI AL; 5 ('auto') (line 286321)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI AL; 5 ('RTL') (line 286321)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI ES; 5 ('auto') (line 286322)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI ES; 5 ('RTL') (line 286322)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI ET; 5 ('auto') (line 286323)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI ET; 5 ('RTL') (line 286323)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI CS; 5 ('auto') (line 286324)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI CS; 5 ('RTL') (line 286324)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI NSM; 5 ('auto') (line 286325)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI NSM; 5 ('RTL') (line 286325)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_032)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // AL RLO RLI ON; 5 ('auto') (line 286326)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO RLI ON; 5 ('RTL') (line 286326)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO FSI R; 5 ('auto') (line 286327)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO FSI R; 5 ('RTL') (line 286327)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO FSI AL; 5 ('auto') (line 286328)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), -1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), -1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, -1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // AL RLO FSI AL; 5 ('RTL') (line 286328)
          std::vector<uint32_t> const cps = { 0x0608, 0x202E, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLE LRI R; 4 ('RTL') (line 286329)
          std::vector<uint32_t> const cps = { 0x002B, 0x202B, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLE LRI AL; 4 ('RTL') (line 286330)
          std::vector<uint32_t> const cps = { 0x002B, 0x202B, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLE RLI R; 4 ('RTL') (line 286331)
          std::vector<uint32_t> const cps = { 0x002B, 0x202B, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLE RLI AL; 4 ('RTL') (line 286332)
          std::vector<uint32_t> const cps = { 0x002B, 0x202B, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLE RLI ES; 4 ('RTL') (line 286333)
          std::vector<uint32_t> const cps = { 0x002B, 0x202B, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLE RLI ET; 4 ('RTL') (line 286334)
          std::vector<uint32_t> const cps = { 0x002B, 0x202B, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLE RLI CS; 4 ('RTL') (line 286335)
          std::vector<uint32_t> const cps = { 0x002B, 0x202B, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_033)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // ES RLE RLI NSM; 4 ('RTL') (line 286336)
          std::vector<uint32_t> const cps = { 0x002B, 0x202B, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLE RLI ON; 4 ('RTL') (line 286337)
          std::vector<uint32_t> const cps = { 0x002B, 0x202B, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLE FSI R; 4 ('RTL') (line 286338)
          std::vector<uint32_t> const cps = { 0x002B, 0x202B, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLE FSI AL; 4 ('RTL') (line 286339)
          std::vector<uint32_t> const cps = { 0x002B, 0x202B, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLO LRI R; 4 ('RTL') (line 286340)
          std::vector<uint32_t> const cps = { 0x002B, 0x202E, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLO LRI AL; 4 ('RTL') (line 286341)
          std::vector<uint32_t> const cps = { 0x002B, 0x202E, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLO RLI R; 4 ('RTL') (line 286342)
          std::vector<uint32_t> const cps = { 0x002B, 0x202E, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLO RLI AL; 4 ('RTL') (line 286343)
          std::vector<uint32_t> const cps = { 0x002B, 0x202E, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLO RLI ES; 4 ('RTL') (line 286344)
          std::vector<uint32_t> const cps = { 0x002B, 0x202E, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLO RLI ET; 4 ('RTL') (line 286345)
          std::vector<uint32_t> const cps = { 0x002B, 0x202E, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_034)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // ES RLO RLI CS; 4 ('RTL') (line 286346)
          std::vector<uint32_t> const cps = { 0x002B, 0x202E, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLO RLI NSM; 4 ('RTL') (line 286347)
          std::vector<uint32_t> const cps = { 0x002B, 0x202E, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLO RLI ON; 4 ('RTL') (line 286348)
          std::vector<uint32_t> const cps = { 0x002B, 0x202E, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLO FSI R; 4 ('RTL') (line 286349)
          std::vector<uint32_t> const cps = { 0x002B, 0x202E, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ES RLO FSI AL; 4 ('RTL') (line 286350)
          std::vector<uint32_t> const cps = { 0x002B, 0x202E, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLE LRI R; 4 ('RTL') (line 286351)
          std::vector<uint32_t> const cps = { 0x0023, 0x202B, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLE LRI AL; 4 ('RTL') (line 286352)
          std::vector<uint32_t> const cps = { 0x0023, 0x202B, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLE RLI R; 4 ('RTL') (line 286353)
          std::vector<uint32_t> const cps = { 0x0023, 0x202B, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLE RLI AL; 4 ('RTL') (line 286354)
          std::vector<uint32_t> const cps = { 0x0023, 0x202B, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLE RLI ES; 4 ('RTL') (line 286355)
          std::vector<uint32_t> const cps = { 0x0023, 0x202B, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_035)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // ET RLE RLI ET; 4 ('RTL') (line 286356)
          std::vector<uint32_t> const cps = { 0x0023, 0x202B, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLE RLI CS; 4 ('RTL') (line 286357)
          std::vector<uint32_t> const cps = { 0x0023, 0x202B, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLE RLI NSM; 4 ('RTL') (line 286358)
          std::vector<uint32_t> const cps = { 0x0023, 0x202B, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLE RLI ON; 4 ('RTL') (line 286359)
          std::vector<uint32_t> const cps = { 0x0023, 0x202B, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLE FSI R; 4 ('RTL') (line 286360)
          std::vector<uint32_t> const cps = { 0x0023, 0x202B, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLE FSI AL; 4 ('RTL') (line 286361)
          std::vector<uint32_t> const cps = { 0x0023, 0x202B, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLO LRI R; 4 ('RTL') (line 286362)
          std::vector<uint32_t> const cps = { 0x0023, 0x202E, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLO LRI AL; 4 ('RTL') (line 286363)
          std::vector<uint32_t> const cps = { 0x0023, 0x202E, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLO RLI R; 4 ('RTL') (line 286364)
          std::vector<uint32_t> const cps = { 0x0023, 0x202E, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLO RLI AL; 4 ('RTL') (line 286365)
          std::vector<uint32_t> const cps = { 0x0023, 0x202E, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_036)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // ET RLO RLI ES; 4 ('RTL') (line 286366)
          std::vector<uint32_t> const cps = { 0x0023, 0x202E, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLO RLI ET; 4 ('RTL') (line 286367)
          std::vector<uint32_t> const cps = { 0x0023, 0x202E, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLO RLI CS; 4 ('RTL') (line 286368)
          std::vector<uint32_t> const cps = { 0x0023, 0x202E, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLO RLI NSM; 4 ('RTL') (line 286369)
          std::vector<uint32_t> const cps = { 0x0023, 0x202E, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLO RLI ON; 4 ('RTL') (line 286370)
          std::vector<uint32_t> const cps = { 0x0023, 0x202E, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLO FSI R; 4 ('RTL') (line 286371)
          std::vector<uint32_t> const cps = { 0x0023, 0x202E, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ET RLO FSI AL; 4 ('RTL') (line 286372)
          std::vector<uint32_t> const cps = { 0x0023, 0x202E, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLE LRI R; 4 ('RTL') (line 286373)
          std::vector<uint32_t> const cps = { 0x002C, 0x202B, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLE LRI AL; 4 ('RTL') (line 286374)
          std::vector<uint32_t> const cps = { 0x002C, 0x202B, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLE RLI R; 4 ('RTL') (line 286375)
          std::vector<uint32_t> const cps = { 0x002C, 0x202B, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_037)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // CS RLE RLI AL; 4 ('RTL') (line 286376)
          std::vector<uint32_t> const cps = { 0x002C, 0x202B, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLE RLI ES; 4 ('RTL') (line 286377)
          std::vector<uint32_t> const cps = { 0x002C, 0x202B, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLE RLI ET; 4 ('RTL') (line 286378)
          std::vector<uint32_t> const cps = { 0x002C, 0x202B, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLE RLI CS; 4 ('RTL') (line 286379)
          std::vector<uint32_t> const cps = { 0x002C, 0x202B, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLE RLI NSM; 4 ('RTL') (line 286380)
          std::vector<uint32_t> const cps = { 0x002C, 0x202B, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLE RLI ON; 4 ('RTL') (line 286381)
          std::vector<uint32_t> const cps = { 0x002C, 0x202B, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLE FSI R; 4 ('RTL') (line 286382)
          std::vector<uint32_t> const cps = { 0x002C, 0x202B, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLE FSI AL; 4 ('RTL') (line 286383)
          std::vector<uint32_t> const cps = { 0x002C, 0x202B, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLO LRI R; 4 ('RTL') (line 286384)
          std::vector<uint32_t> const cps = { 0x002C, 0x202E, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLO LRI AL; 4 ('RTL') (line 286385)
          std::vector<uint32_t> const cps = { 0x002C, 0x202E, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_038)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // CS RLO RLI R; 4 ('RTL') (line 286386)
          std::vector<uint32_t> const cps = { 0x002C, 0x202E, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLO RLI AL; 4 ('RTL') (line 286387)
          std::vector<uint32_t> const cps = { 0x002C, 0x202E, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLO RLI ES; 4 ('RTL') (line 286388)
          std::vector<uint32_t> const cps = { 0x002C, 0x202E, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLO RLI ET; 4 ('RTL') (line 286389)
          std::vector<uint32_t> const cps = { 0x002C, 0x202E, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLO RLI CS; 4 ('RTL') (line 286390)
          std::vector<uint32_t> const cps = { 0x002C, 0x202E, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLO RLI NSM; 4 ('RTL') (line 286391)
          std::vector<uint32_t> const cps = { 0x002C, 0x202E, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLO RLI ON; 4 ('RTL') (line 286392)
          std::vector<uint32_t> const cps = { 0x002C, 0x202E, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLO FSI R; 4 ('RTL') (line 286393)
          std::vector<uint32_t> const cps = { 0x002C, 0x202E, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // CS RLO FSI AL; 4 ('RTL') (line 286394)
          std::vector<uint32_t> const cps = { 0x002C, 0x202E, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLE LRI R; 4 ('RTL') (line 286395)
          std::vector<uint32_t> const cps = { 0x0300, 0x202B, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_039)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // NSM RLE LRI AL; 4 ('RTL') (line 286396)
          std::vector<uint32_t> const cps = { 0x0300, 0x202B, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLE RLI R; 4 ('RTL') (line 286397)
          std::vector<uint32_t> const cps = { 0x0300, 0x202B, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLE RLI AL; 4 ('RTL') (line 286398)
          std::vector<uint32_t> const cps = { 0x0300, 0x202B, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLE RLI ES; 4 ('RTL') (line 286399)
          std::vector<uint32_t> const cps = { 0x0300, 0x202B, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLE RLI ET; 4 ('RTL') (line 286400)
          std::vector<uint32_t> const cps = { 0x0300, 0x202B, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLE RLI CS; 4 ('RTL') (line 286401)
          std::vector<uint32_t> const cps = { 0x0300, 0x202B, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLE RLI NSM; 4 ('RTL') (line 286402)
          std::vector<uint32_t> const cps = { 0x0300, 0x202B, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLE RLI ON; 4 ('RTL') (line 286403)
          std::vector<uint32_t> const cps = { 0x0300, 0x202B, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLE FSI R; 4 ('RTL') (line 286404)
          std::vector<uint32_t> const cps = { 0x0300, 0x202B, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLE FSI AL; 4 ('RTL') (line 286405)
          std::vector<uint32_t> const cps = { 0x0300, 0x202B, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_040)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // NSM RLO LRI R; 4 ('RTL') (line 286406)
          std::vector<uint32_t> const cps = { 0x0300, 0x202E, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLO LRI AL; 4 ('RTL') (line 286407)
          std::vector<uint32_t> const cps = { 0x0300, 0x202E, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLO RLI R; 4 ('RTL') (line 286408)
          std::vector<uint32_t> const cps = { 0x0300, 0x202E, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLO RLI AL; 4 ('RTL') (line 286409)
          std::vector<uint32_t> const cps = { 0x0300, 0x202E, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLO RLI ES; 4 ('RTL') (line 286410)
          std::vector<uint32_t> const cps = { 0x0300, 0x202E, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLO RLI ET; 4 ('RTL') (line 286411)
          std::vector<uint32_t> const cps = { 0x0300, 0x202E, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLO RLI CS; 4 ('RTL') (line 286412)
          std::vector<uint32_t> const cps = { 0x0300, 0x202E, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLO RLI NSM; 4 ('RTL') (line 286413)
          std::vector<uint32_t> const cps = { 0x0300, 0x202E, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLO RLI ON; 4 ('RTL') (line 286414)
          std::vector<uint32_t> const cps = { 0x0300, 0x202E, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // NSM RLO FSI R; 4 ('RTL') (line 286415)
          std::vector<uint32_t> const cps = { 0x0300, 0x202E, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_041)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // NSM RLO FSI AL; 4 ('RTL') (line 286416)
          std::vector<uint32_t> const cps = { 0x0300, 0x202E, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLE LRI R; 4 ('RTL') (line 286417)
          std::vector<uint32_t> const cps = { 0x0009, 0x202B, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLE LRI AL; 4 ('RTL') (line 286418)
          std::vector<uint32_t> const cps = { 0x0009, 0x202B, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLE RLI R; 4 ('RTL') (line 286419)
          std::vector<uint32_t> const cps = { 0x0009, 0x202B, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLE RLI AL; 4 ('RTL') (line 286420)
          std::vector<uint32_t> const cps = { 0x0009, 0x202B, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLE RLI ES; 4 ('RTL') (line 286421)
          std::vector<uint32_t> const cps = { 0x0009, 0x202B, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLE RLI ET; 4 ('RTL') (line 286422)
          std::vector<uint32_t> const cps = { 0x0009, 0x202B, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLE RLI CS; 4 ('RTL') (line 286423)
          std::vector<uint32_t> const cps = { 0x0009, 0x202B, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLE RLI NSM; 4 ('RTL') (line 286424)
          std::vector<uint32_t> const cps = { 0x0009, 0x202B, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLE RLI ON; 4 ('RTL') (line 286425)
          std::vector<uint32_t> const cps = { 0x0009, 0x202B, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_042)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // S RLE FSI R; 4 ('RTL') (line 286426)
          std::vector<uint32_t> const cps = { 0x0009, 0x202B, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLE FSI AL; 4 ('RTL') (line 286427)
          std::vector<uint32_t> const cps = { 0x0009, 0x202B, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLO LRI R; 4 ('RTL') (line 286428)
          std::vector<uint32_t> const cps = { 0x0009, 0x202E, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLO LRI AL; 4 ('RTL') (line 286429)
          std::vector<uint32_t> const cps = { 0x0009, 0x202E, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLO RLI R; 4 ('RTL') (line 286430)
          std::vector<uint32_t> const cps = { 0x0009, 0x202E, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLO RLI AL; 4 ('RTL') (line 286431)
          std::vector<uint32_t> const cps = { 0x0009, 0x202E, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLO RLI ES; 4 ('RTL') (line 286432)
          std::vector<uint32_t> const cps = { 0x0009, 0x202E, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLO RLI ET; 4 ('RTL') (line 286433)
          std::vector<uint32_t> const cps = { 0x0009, 0x202E, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLO RLI CS; 4 ('RTL') (line 286434)
          std::vector<uint32_t> const cps = { 0x0009, 0x202E, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLO RLI NSM; 4 ('RTL') (line 286435)
          std::vector<uint32_t> const cps = { 0x0009, 0x202E, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_043)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // S RLO RLI ON; 4 ('RTL') (line 286436)
          std::vector<uint32_t> const cps = { 0x0009, 0x202E, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLO FSI R; 4 ('RTL') (line 286437)
          std::vector<uint32_t> const cps = { 0x0009, 0x202E, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // S RLO FSI AL; 4 ('RTL') (line 286438)
          std::vector<uint32_t> const cps = { 0x0009, 0x202E, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLE LRI R; 4 ('RTL') (line 286439)
          std::vector<uint32_t> const cps = { 0x0020, 0x202B, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLE LRI AL; 4 ('RTL') (line 286440)
          std::vector<uint32_t> const cps = { 0x0020, 0x202B, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLE RLI R; 4 ('RTL') (line 286441)
          std::vector<uint32_t> const cps = { 0x0020, 0x202B, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLE RLI AL; 4 ('RTL') (line 286442)
          std::vector<uint32_t> const cps = { 0x0020, 0x202B, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLE RLI ES; 4 ('RTL') (line 286443)
          std::vector<uint32_t> const cps = { 0x0020, 0x202B, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLE RLI ET; 4 ('RTL') (line 286444)
          std::vector<uint32_t> const cps = { 0x0020, 0x202B, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLE RLI CS; 4 ('RTL') (line 286445)
          std::vector<uint32_t> const cps = { 0x0020, 0x202B, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_044)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // WS RLE RLI NSM; 4 ('RTL') (line 286446)
          std::vector<uint32_t> const cps = { 0x0020, 0x202B, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLE RLI ON; 4 ('RTL') (line 286447)
          std::vector<uint32_t> const cps = { 0x0020, 0x202B, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLE FSI R; 4 ('RTL') (line 286448)
          std::vector<uint32_t> const cps = { 0x0020, 0x202B, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLE FSI AL; 4 ('RTL') (line 286449)
          std::vector<uint32_t> const cps = { 0x0020, 0x202B, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLO LRI R; 4 ('RTL') (line 286450)
          std::vector<uint32_t> const cps = { 0x0020, 0x202E, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLO LRI AL; 4 ('RTL') (line 286451)
          std::vector<uint32_t> const cps = { 0x0020, 0x202E, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLO RLI R; 4 ('RTL') (line 286452)
          std::vector<uint32_t> const cps = { 0x0020, 0x202E, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLO RLI AL; 4 ('RTL') (line 286453)
          std::vector<uint32_t> const cps = { 0x0020, 0x202E, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLO RLI ES; 4 ('RTL') (line 286454)
          std::vector<uint32_t> const cps = { 0x0020, 0x202E, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLO RLI ET; 4 ('RTL') (line 286455)
          std::vector<uint32_t> const cps = { 0x0020, 0x202E, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_045)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // WS RLO RLI CS; 4 ('RTL') (line 286456)
          std::vector<uint32_t> const cps = { 0x0020, 0x202E, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLO RLI NSM; 4 ('RTL') (line 286457)
          std::vector<uint32_t> const cps = { 0x0020, 0x202E, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLO RLI ON; 4 ('RTL') (line 286458)
          std::vector<uint32_t> const cps = { 0x0020, 0x202E, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLO FSI R; 4 ('RTL') (line 286459)
          std::vector<uint32_t> const cps = { 0x0020, 0x202E, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // WS RLO FSI AL; 4 ('RTL') (line 286460)
          std::vector<uint32_t> const cps = { 0x0020, 0x202E, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE LRI R; 4 ('RTL') (line 286461)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE LRI AL; 4 ('RTL') (line 286462)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE RLI R; 4 ('RTL') (line 286463)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE RLI AL; 4 ('RTL') (line 286464)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE RLI ES; 4 ('RTL') (line 286465)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_046)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // ON RLE RLI ET; 4 ('RTL') (line 286466)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE RLI CS; 4 ('RTL') (line 286467)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE RLI NSM; 4 ('RTL') (line 286468)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE RLI ON; 4 ('RTL') (line 286469)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE FSI R; 4 ('RTL') (line 286470)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLE FSI AL; 4 ('RTL') (line 286471)
          std::vector<uint32_t> const cps = { 0x0021, 0x202B, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO LRI R; 4 ('RTL') (line 286472)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO LRI AL; 4 ('RTL') (line 286473)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO RLI R; 4 ('RTL') (line 286474)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO RLI AL; 4 ('RTL') (line 286475)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_047)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // ON RLO RLI ES; 4 ('RTL') (line 286476)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO RLI ET; 4 ('RTL') (line 286477)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO RLI CS; 4 ('RTL') (line 286478)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO RLI NSM; 4 ('RTL') (line 286479)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO RLI ON; 4 ('RTL') (line 286480)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO FSI R; 4 ('RTL') (line 286481)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // ON RLO FSI AL; 4 ('RTL') (line 286482)
          std::vector<uint32_t> const cps = { 0x0021, 0x202E, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE LRI R; 4 ('RTL') (line 286483)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE LRI AL; 4 ('RTL') (line 286484)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE RLI R; 4 ('RTL') (line 286485)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_048)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // LRI RLE RLI AL; 4 ('RTL') (line 286486)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE RLI ES; 4 ('RTL') (line 286487)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE RLI ET; 4 ('RTL') (line 286488)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE RLI CS; 4 ('RTL') (line 286489)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE RLI NSM; 4 ('RTL') (line 286490)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2067, 0x0300 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE RLI ON; 4 ('RTL') (line 286491)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2067, 0x0021 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE FSI R; 4 ('RTL') (line 286492)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2068, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLE FSI AL; 4 ('RTL') (line 286493)
          std::vector<uint32_t> const cps = { 0x2066, 0x202B, 0x2068, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO LRI R; 4 ('RTL') (line 286494)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2066, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO LRI AL; 4 ('RTL') (line 286495)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2066, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  }
  
  TEST(bidi, bidi_572_049)
  {
  
      expected_levels = { 1, -1, 3, 5 };
      expected_reordered_indices = { 3, 2, 0 };
  
      {
          // LRI RLO RLI R; 4 ('RTL') (line 286496)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2067, 0x05BE };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO RLI AL; 4 ('RTL') (line 286497)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2067, 0x0608 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO RLI ES; 4 ('RTL') (line 286498)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2067, 0x002B };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO RLI ET; 4 ('RTL') (line 286499)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2067, 0x0023 };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
      {
          // LRI RLO RLI CS; 4 ('RTL') (line 286500)
          std::vector<uint32_t> const cps = { 0x2066, 0x202E, 0x2067, 0x002C };
          std::vector<int> const levels =
              bidi_levels(cps.begin(), cps.end(), 1);
          int i = 0;
          for (int l : expected_levels) {
              if (0 <= l) {
                  EXPECT_EQ(levels[i], l) << "i=" << i;
                  ++i;
              }
          }
          EXPECT_EQ((int)levels.size(), i);
  
          std::vector<int> const reordered =
              bidi_reordered_indices(cps.begin(), cps.end(), 1);
          i = 0;
          for (int idx : expected_reordered_indices) {
              // Skip FSI, LRI, RLI, and PDI.
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
  
          std::vector<int> reordered_2;
          for (auto subrange :
              boost::text::bidirectional_subranges(cps, 1)) {
              for (auto cp : subrange) {
                  reordered_2.push_back(cp);
              }
          }
          i = 0;
          for (int idx : expected_reordered_indices) {
              if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                  EXPECT_EQ(reordered_2[i], (int)cps[idx])
                      << std::hex
                      << " 0x" << reordered_2[i]
                      << " 0x" << cps[idx]
                      << std::dec << " i=" << i;
              }
              ++i;
          }
          EXPECT_EQ(i, (int)reordered_2.size());
      }
  
  }
  