<reponame>eightysquirrels/text<filename>test/bidi_test_611.cpp
// 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_611_000)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // L BN NSM CS; 4 ('RTL') (line 305501)
        std::vector<uint32_t> const cps = { 0x0041, 0x00AD, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // L BN NSM B; 4 ('RTL') (line 305502)
        std::vector<uint32_t> const cps = { 0x0041, 0x00AD, 0x0300, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // L BN NSM S; 4 ('RTL') (line 305503)
        std::vector<uint32_t> const cps = { 0x0041, 0x00AD, 0x0300, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // L BN NSM WS; 4 ('RTL') (line 305504)
        std::vector<uint32_t> const cps = { 0x0041, 0x00AD, 0x0300, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // L BN NSM ON; 4 ('RTL') (line 305505)
        std::vector<uint32_t> const cps = { 0x0041, 0x00AD, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // L BN NSM LRI; 4 ('RTL') (line 305506)
        std::vector<uint32_t> const cps = { 0x0041, 0x00AD, 0x0300, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // L BN NSM RLI; 4 ('RTL') (line 305507)
        std::vector<uint32_t> const cps = { 0x0041, 0x00AD, 0x0300, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // L BN NSM FSI; 4 ('RTL') (line 305508)
        std::vector<uint32_t> const cps = { 0x0041, 0x00AD, 0x0300, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // L BN NSM PDI; 4 ('RTL') (line 305509)
        std::vector<uint32_t> const cps = { 0x0041, 0x00AD, 0x0300, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE L B; 4 ('RTL') (line 305510)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0041, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_001)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN LRE L S; 4 ('RTL') (line 305511)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0041, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE L WS; 4 ('RTL') (line 305512)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0041, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE L LRI; 4 ('RTL') (line 305513)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0041, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE L RLI; 4 ('RTL') (line 305514)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0041, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE L FSI; 4 ('RTL') (line 305515)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0041, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE L PDI; 4 ('RTL') (line 305516)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0041, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE EN B; 4 ('RTL') (line 305517)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0030, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE EN S; 4 ('RTL') (line 305518)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0030, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE EN WS; 4 ('RTL') (line 305519)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0030, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE EN LRI; 4 ('RTL') (line 305520)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0030, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_002)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN LRE EN RLI; 4 ('RTL') (line 305521)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0030, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE EN FSI; 4 ('RTL') (line 305522)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0030, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE EN PDI; 4 ('RTL') (line 305523)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0030, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ES B; 4 ('RTL') (line 305524)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002B, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ES S; 4 ('RTL') (line 305525)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002B, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ES WS; 4 ('RTL') (line 305526)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002B, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ES LRI; 4 ('RTL') (line 305527)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002B, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ES RLI; 4 ('RTL') (line 305528)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002B, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ES FSI; 4 ('RTL') (line 305529)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002B, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ES PDI; 4 ('RTL') (line 305530)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002B, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_003)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN LRE ET B; 4 ('RTL') (line 305531)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0023, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ET S; 4 ('RTL') (line 305532)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0023, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ET WS; 4 ('RTL') (line 305533)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0023, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ET LRI; 4 ('RTL') (line 305534)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0023, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ET RLI; 4 ('RTL') (line 305535)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0023, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ET FSI; 4 ('RTL') (line 305536)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0023, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ET PDI; 4 ('RTL') (line 305537)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0023, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE CS B; 4 ('RTL') (line 305538)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002C, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE CS S; 4 ('RTL') (line 305539)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002C, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE CS WS; 4 ('RTL') (line 305540)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002C, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_004)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN LRE CS LRI; 4 ('RTL') (line 305541)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002C, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE CS RLI; 4 ('RTL') (line 305542)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002C, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE CS FSI; 4 ('RTL') (line 305543)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002C, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE CS PDI; 4 ('RTL') (line 305544)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x002C, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE NSM B; 4 ('RTL') (line 305545)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0300, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE NSM S; 4 ('RTL') (line 305546)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0300, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE NSM WS; 4 ('RTL') (line 305547)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0300, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE NSM LRI; 4 ('RTL') (line 305548)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0300, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE NSM RLI; 4 ('RTL') (line 305549)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0300, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE NSM FSI; 4 ('RTL') (line 305550)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0300, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_005)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN LRE NSM PDI; 4 ('RTL') (line 305551)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0300, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ON B; 4 ('RTL') (line 305552)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0021, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ON S; 4 ('RTL') (line 305553)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0021, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ON WS; 4 ('RTL') (line 305554)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0021, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ON LRI; 4 ('RTL') (line 305555)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0021, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ON RLI; 4 ('RTL') (line 305556)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0021, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ON FSI; 4 ('RTL') (line 305557)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0021, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRE ON PDI; 4 ('RTL') (line 305558)
        std::vector<uint32_t> const cps = { 0x0030, 0x202A, 0x0021, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO L B; 4 ('RTL') (line 305559)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0041, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO L S; 4 ('RTL') (line 305560)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0041, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_006)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN LRO L WS; 4 ('RTL') (line 305561)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0041, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO L LRI; 4 ('RTL') (line 305562)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0041, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO L RLI; 4 ('RTL') (line 305563)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0041, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO L FSI; 4 ('RTL') (line 305564)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0041, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO L PDI; 4 ('RTL') (line 305565)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0041, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R B; 5 ('auto') (line 305566)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R B; 5 ('RTL') (line 305566)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R S; 5 ('auto') (line 305567)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R S; 5 ('RTL') (line 305567)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R WS; 5 ('auto') (line 305568)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R WS; 5 ('RTL') (line 305568)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R LRI; 5 ('auto') (line 305569)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R LRI; 5 ('RTL') (line 305569)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R RLI; 5 ('auto') (line 305570)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R RLI; 5 ('RTL') (line 305570)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_007)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN LRO R FSI; 5 ('auto') (line 305571)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R FSI; 5 ('RTL') (line 305571)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R PDI; 5 ('auto') (line 305572)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO R PDI; 5 ('RTL') (line 305572)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x05BE, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL B; 5 ('auto') (line 305573)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL B; 5 ('RTL') (line 305573)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL S; 5 ('auto') (line 305574)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL S; 5 ('RTL') (line 305574)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL WS; 5 ('auto') (line 305575)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL WS; 5 ('RTL') (line 305575)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL LRI; 5 ('auto') (line 305576)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL LRI; 5 ('RTL') (line 305576)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL RLI; 5 ('auto') (line 305577)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL RLI; 5 ('RTL') (line 305577)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL FSI; 5 ('auto') (line 305578)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL FSI; 5 ('RTL') (line 305578)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL PDI; 5 ('auto') (line 305579)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AL PDI; 5 ('RTL') (line 305579)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0608, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO EN B; 4 ('RTL') (line 305580)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0030, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_008)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN LRO EN S; 4 ('RTL') (line 305581)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0030, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO EN WS; 4 ('RTL') (line 305582)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0030, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO EN LRI; 4 ('RTL') (line 305583)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0030, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO EN RLI; 4 ('RTL') (line 305584)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0030, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO EN FSI; 4 ('RTL') (line 305585)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0030, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO EN PDI; 4 ('RTL') (line 305586)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0030, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ES B; 4 ('RTL') (line 305587)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002B, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ES S; 4 ('RTL') (line 305588)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002B, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ES WS; 4 ('RTL') (line 305589)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002B, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ES LRI; 4 ('RTL') (line 305590)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002B, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_009)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN LRO ES RLI; 4 ('RTL') (line 305591)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002B, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ES FSI; 4 ('RTL') (line 305592)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002B, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ES PDI; 4 ('RTL') (line 305593)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002B, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ET B; 4 ('RTL') (line 305594)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0023, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ET S; 4 ('RTL') (line 305595)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0023, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ET WS; 4 ('RTL') (line 305596)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0023, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ET LRI; 4 ('RTL') (line 305597)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0023, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ET RLI; 4 ('RTL') (line 305598)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0023, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ET FSI; 4 ('RTL') (line 305599)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0023, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ET PDI; 4 ('RTL') (line 305600)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0023, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_010)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN LRO AN B; 4 ('RTL') (line 305601)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0660, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AN S; 4 ('RTL') (line 305602)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0660, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AN WS; 4 ('RTL') (line 305603)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0660, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AN LRI; 4 ('RTL') (line 305604)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0660, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AN RLI; 4 ('RTL') (line 305605)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0660, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AN FSI; 4 ('RTL') (line 305606)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0660, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO AN PDI; 4 ('RTL') (line 305607)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0660, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO CS B; 4 ('RTL') (line 305608)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002C, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO CS S; 4 ('RTL') (line 305609)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002C, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO CS WS; 4 ('RTL') (line 305610)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002C, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_011)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN LRO CS LRI; 4 ('RTL') (line 305611)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002C, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO CS RLI; 4 ('RTL') (line 305612)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002C, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO CS FSI; 4 ('RTL') (line 305613)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002C, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO CS PDI; 4 ('RTL') (line 305614)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x002C, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO NSM B; 4 ('RTL') (line 305615)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0300, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO NSM S; 4 ('RTL') (line 305616)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0300, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO NSM WS; 4 ('RTL') (line 305617)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0300, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO NSM LRI; 4 ('RTL') (line 305618)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0300, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO NSM RLI; 4 ('RTL') (line 305619)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0300, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO NSM FSI; 4 ('RTL') (line 305620)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0300, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_012)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN LRO NSM PDI; 4 ('RTL') (line 305621)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0300, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ON B; 4 ('RTL') (line 305622)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0021, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ON S; 4 ('RTL') (line 305623)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0021, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ON WS; 4 ('RTL') (line 305624)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0021, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ON LRI; 4 ('RTL') (line 305625)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0021, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ON RLI; 4 ('RTL') (line 305626)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0021, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ON FSI; 4 ('RTL') (line 305627)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0021, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN LRO ON PDI; 4 ('RTL') (line 305628)
        std::vector<uint32_t> const cps = { 0x0030, 0x202D, 0x0021, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF L R; 4 ('RTL') (line 305629)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF L AL; 4 ('RTL') (line 305630)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_013)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN PDF L ES; 4 ('RTL') (line 305631)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF L ET; 4 ('RTL') (line 305632)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF L CS; 4 ('RTL') (line 305633)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF L B; 4 ('RTL') (line 305634)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF L S; 4 ('RTL') (line 305635)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF L WS; 4 ('RTL') (line 305636)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF L ON; 4 ('RTL') (line 305637)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF L LRI; 4 ('RTL') (line 305638)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF L RLI; 4 ('RTL') (line 305639)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF L FSI; 4 ('RTL') (line 305640)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_014)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN PDF L PDI; 4 ('RTL') (line 305641)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0041, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN R; 5 ('auto') (line 305642)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN R; 5 ('RTL') (line 305642)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN AL; 5 ('auto') (line 305643)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN AL; 5 ('RTL') (line 305643)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN ES; 4 ('RTL') (line 305644)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN CS; 4 ('RTL') (line 305645)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN B; 4 ('RTL') (line 305646)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN S; 4 ('RTL') (line 305647)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN WS; 4 ('RTL') (line 305648)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN ON; 4 ('RTL') (line 305649)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN LRI; 4 ('RTL') (line 305650)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_015)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN PDF EN RLI; 4 ('RTL') (line 305651)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN FSI; 4 ('RTL') (line 305652)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF EN PDI; 4 ('RTL') (line 305653)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0030, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET R; 5 ('auto') (line 305654)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET R; 5 ('RTL') (line 305654)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET AL; 5 ('auto') (line 305655)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET AL; 5 ('RTL') (line 305655)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET ES; 4 ('RTL') (line 305656)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET CS; 4 ('RTL') (line 305657)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET B; 4 ('RTL') (line 305658)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET S; 4 ('RTL') (line 305659)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET WS; 4 ('RTL') (line 305660)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_016)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN PDF ET ON; 4 ('RTL') (line 305661)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET LRI; 4 ('RTL') (line 305662)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET RLI; 4 ('RTL') (line 305663)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET FSI; 4 ('RTL') (line 305664)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF ET PDI; 4 ('RTL') (line 305665)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0023, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN R; 5 ('auto') (line 305666)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN R; 5 ('RTL') (line 305666)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN AL; 5 ('auto') (line 305667)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN AL; 5 ('RTL') (line 305667)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN ES; 4 ('RTL') (line 305668)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN ET; 4 ('RTL') (line 305669)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN CS; 4 ('RTL') (line 305670)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_017)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN PDF AN B; 4 ('RTL') (line 305671)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN S; 4 ('RTL') (line 305672)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN WS; 4 ('RTL') (line 305673)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN ON; 4 ('RTL') (line 305674)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN LRI; 4 ('RTL') (line 305675)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN RLI; 4 ('RTL') (line 305676)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN FSI; 4 ('RTL') (line 305677)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF AN PDI; 4 ('RTL') (line 305678)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0660, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM R; 5 ('auto') (line 305679)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM R; 5 ('RTL') (line 305679)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM AL; 5 ('auto') (line 305680)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM AL; 5 ('RTL') (line 305680)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_018)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN PDF NSM ES; 4 ('RTL') (line 305681)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM CS; 4 ('RTL') (line 305682)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM B; 4 ('RTL') (line 305683)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM S; 4 ('RTL') (line 305684)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM WS; 4 ('RTL') (line 305685)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM ON; 4 ('RTL') (line 305686)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM LRI; 4 ('RTL') (line 305687)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM RLI; 4 ('RTL') (line 305688)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM FSI; 4 ('RTL') (line 305689)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN PDF NSM PDI; 4 ('RTL') (line 305690)
        std::vector<uint32_t> const cps = { 0x0030, 0x202C, 0x0300, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_019)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN BN L R; 4 ('RTL') (line 305691)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN L AL; 4 ('RTL') (line 305692)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN L ES; 4 ('RTL') (line 305693)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN L ET; 4 ('RTL') (line 305694)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN L CS; 4 ('RTL') (line 305695)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN L B; 4 ('RTL') (line 305696)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN L S; 4 ('RTL') (line 305697)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN L WS; 4 ('RTL') (line 305698)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN L ON; 4 ('RTL') (line 305699)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN L LRI; 4 ('RTL') (line 305700)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_020)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN BN L RLI; 4 ('RTL') (line 305701)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN L FSI; 4 ('RTL') (line 305702)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN L PDI; 4 ('RTL') (line 305703)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0041, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN R; 5 ('auto') (line 305704)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN R; 5 ('RTL') (line 305704)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN AL; 5 ('auto') (line 305705)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN AL; 5 ('RTL') (line 305705)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN ES; 4 ('RTL') (line 305706)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN CS; 4 ('RTL') (line 305707)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN B; 4 ('RTL') (line 305708)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN S; 4 ('RTL') (line 305709)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN WS; 4 ('RTL') (line 305710)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_021)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN BN EN ON; 4 ('RTL') (line 305711)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN LRI; 4 ('RTL') (line 305712)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN RLI; 4 ('RTL') (line 305713)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN FSI; 4 ('RTL') (line 305714)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN EN PDI; 4 ('RTL') (line 305715)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0030, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET R; 5 ('auto') (line 305716)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET R; 5 ('RTL') (line 305716)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET AL; 5 ('auto') (line 305717)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET AL; 5 ('RTL') (line 305717)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET ES; 4 ('RTL') (line 305718)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET CS; 4 ('RTL') (line 305719)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET B; 4 ('RTL') (line 305720)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_022)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN BN ET S; 4 ('RTL') (line 305721)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET WS; 4 ('RTL') (line 305722)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET ON; 4 ('RTL') (line 305723)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET LRI; 4 ('RTL') (line 305724)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET RLI; 4 ('RTL') (line 305725)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET FSI; 4 ('RTL') (line 305726)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN ET PDI; 4 ('RTL') (line 305727)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0023, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN AN R; 5 ('auto') (line 305728)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN AN R; 5 ('RTL') (line 305728)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // <NAME>; 5 ('auto') (line 305729)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // <NAME>; 5 ('RTL') (line 305729)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN AN ES; 4 ('RTL') (line 305730)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_023)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN BN AN ET; 4 ('RTL') (line 305731)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN AN CS; 4 ('RTL') (line 305732)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN AN B; 4 ('RTL') (line 305733)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN AN S; 4 ('RTL') (line 305734)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN AN WS; 4 ('RTL') (line 305735)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN AN ON; 4 ('RTL') (line 305736)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN AN LRI; 4 ('RTL') (line 305737)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN AN RLI; 4 ('RTL') (line 305738)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN AN FSI; 4 ('RTL') (line 305739)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN AN PDI; 4 ('RTL') (line 305740)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0660, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_024)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN BN NSM R; 5 ('auto') (line 305741)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN NSM R; 5 ('RTL') (line 305741)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN NSM AL; 5 ('auto') (line 305742)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN NSM AL; 5 ('RTL') (line 305742)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN NSM ES; 4 ('RTL') (line 305743)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN NSM CS; 4 ('RTL') (line 305744)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN NSM B; 4 ('RTL') (line 305745)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN NSM S; 4 ('RTL') (line 305746)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN NSM WS; 4 ('RTL') (line 305747)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN NSM ON; 4 ('RTL') (line 305748)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN NSM LRI; 4 ('RTL') (line 305749)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN NSM RLI; 4 ('RTL') (line 305750)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_025)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // EN BN NSM FSI; 4 ('RTL') (line 305751)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // EN BN NSM PDI; 4 ('RTL') (line 305752)
        std::vector<uint32_t> const cps = { 0x0030, 0x00AD, 0x0300, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN R; 5 ('auto') (line 305753)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN R; 5 ('RTL') (line 305753)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN AL; 5 ('auto') (line 305754)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN AL; 5 ('RTL') (line 305754)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN ES; 4 ('RTL') (line 305755)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN CS; 4 ('RTL') (line 305756)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN B; 4 ('RTL') (line 305757)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN S; 4 ('RTL') (line 305758)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN WS; 4 ('RTL') (line 305759)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN ON; 4 ('RTL') (line 305760)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_026)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // ET PDF EN LRI; 4 ('RTL') (line 305761)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN RLI; 4 ('RTL') (line 305762)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN FSI; 4 ('RTL') (line 305763)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET PDF EN PDI; 4 ('RTL') (line 305764)
        std::vector<uint32_t> const cps = { 0x0023, 0x202C, 0x0030, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN R; 5 ('auto') (line 305765)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN R; 5 ('RTL') (line 305765)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN AL; 5 ('auto') (line 305766)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN AL; 5 ('RTL') (line 305766)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN ES; 4 ('RTL') (line 305767)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN CS; 4 ('RTL') (line 305768)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN B; 4 ('RTL') (line 305769)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN S; 4 ('RTL') (line 305770)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_027)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // ET BN EN WS; 4 ('RTL') (line 305771)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN ON; 4 ('RTL') (line 305772)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN LRI; 4 ('RTL') (line 305773)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN RLI; 4 ('RTL') (line 305774)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN FSI; 4 ('RTL') (line 305775)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // ET BN EN PDI; 4 ('RTL') (line 305776)
        std::vector<uint32_t> const cps = { 0x0023, 0x00AD, 0x0030, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE L B; 4 ('RTL') (line 305777)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0041, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE L S; 4 ('RTL') (line 305778)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0041, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE L WS; 4 ('RTL') (line 305779)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0041, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE L LRI; 4 ('RTL') (line 305780)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0041, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_028)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN LRE L RLI; 4 ('RTL') (line 305781)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0041, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE L FSI; 4 ('RTL') (line 305782)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0041, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE L PDI; 4 ('RTL') (line 305783)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0041, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE EN B; 4 ('RTL') (line 305784)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0030, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE EN S; 4 ('RTL') (line 305785)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0030, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE EN WS; 4 ('RTL') (line 305786)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0030, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE EN LRI; 4 ('RTL') (line 305787)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0030, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE EN RLI; 4 ('RTL') (line 305788)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0030, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE EN FSI; 4 ('RTL') (line 305789)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0030, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE EN PDI; 4 ('RTL') (line 305790)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0030, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_029)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN LRE ES B; 4 ('RTL') (line 305791)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002B, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ES S; 4 ('RTL') (line 305792)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002B, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ES WS; 4 ('RTL') (line 305793)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002B, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ES LRI; 4 ('RTL') (line 305794)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002B, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ES RLI; 4 ('RTL') (line 305795)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002B, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ES FSI; 4 ('RTL') (line 305796)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002B, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ES PDI; 4 ('RTL') (line 305797)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002B, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ET B; 4 ('RTL') (line 305798)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0023, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ET S; 4 ('RTL') (line 305799)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0023, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ET WS; 4 ('RTL') (line 305800)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0023, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_030)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN LRE ET LRI; 4 ('RTL') (line 305801)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0023, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ET RLI; 4 ('RTL') (line 305802)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0023, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ET FSI; 4 ('RTL') (line 305803)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0023, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ET PDI; 4 ('RTL') (line 305804)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0023, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE CS B; 4 ('RTL') (line 305805)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002C, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE CS S; 4 ('RTL') (line 305806)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002C, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE CS WS; 4 ('RTL') (line 305807)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002C, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE CS LRI; 4 ('RTL') (line 305808)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002C, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE CS RLI; 4 ('RTL') (line 305809)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002C, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE CS FSI; 4 ('RTL') (line 305810)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002C, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_031)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN LRE CS PDI; 4 ('RTL') (line 305811)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x002C, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE NSM B; 4 ('RTL') (line 305812)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0300, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE NSM S; 4 ('RTL') (line 305813)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0300, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE NSM WS; 4 ('RTL') (line 305814)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0300, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE NSM LRI; 4 ('RTL') (line 305815)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0300, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE NSM RLI; 4 ('RTL') (line 305816)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0300, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE NSM FSI; 4 ('RTL') (line 305817)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0300, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE NSM PDI; 4 ('RTL') (line 305818)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0300, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ON B; 4 ('RTL') (line 305819)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0021, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ON S; 4 ('RTL') (line 305820)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0021, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_032)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN LRE ON WS; 4 ('RTL') (line 305821)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0021, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ON LRI; 4 ('RTL') (line 305822)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0021, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ON RLI; 4 ('RTL') (line 305823)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0021, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ON FSI; 4 ('RTL') (line 305824)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0021, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRE ON PDI; 4 ('RTL') (line 305825)
        std::vector<uint32_t> const cps = { 0x0660, 0x202A, 0x0021, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO L B; 4 ('RTL') (line 305826)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0041, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO L S; 4 ('RTL') (line 305827)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0041, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO L WS; 4 ('RTL') (line 305828)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0041, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO L LRI; 4 ('RTL') (line 305829)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0041, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO L RLI; 4 ('RTL') (line 305830)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0041, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_033)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN LRO L FSI; 4 ('RTL') (line 305831)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0041, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO L PDI; 4 ('RTL') (line 305832)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0041, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R B; 5 ('auto') (line 305833)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R B; 5 ('RTL') (line 305833)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R S; 5 ('auto') (line 305834)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R S; 5 ('RTL') (line 305834)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R WS; 5 ('auto') (line 305835)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R WS; 5 ('RTL') (line 305835)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R LRI; 5 ('auto') (line 305836)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R LRI; 5 ('RTL') (line 305836)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R RLI; 5 ('auto') (line 305837)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R RLI; 5 ('RTL') (line 305837)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R FSI; 5 ('auto') (line 305838)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R FSI; 5 ('RTL') (line 305838)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R PDI; 5 ('auto') (line 305839)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO R PDI; 5 ('RTL') (line 305839)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x05BE, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AL B; 5 ('auto') (line 305840)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AL B; 5 ('RTL') (line 305840)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_034)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN LRO AL S; 5 ('auto') (line 305841)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AL S; 5 ('RTL') (line 305841)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AL WS; 5 ('auto') (line 305842)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AL WS; 5 ('RTL') (line 305842)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AL LRI; 5 ('auto') (line 305843)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AL LRI; 5 ('RTL') (line 305843)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // <NAME>LI; 5 ('auto') (line 305844)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AL RLI; 5 ('RTL') (line 305844)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AL FSI; 5 ('auto') (line 305845)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AL FSI; 5 ('RTL') (line 305845)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AL PDI; 5 ('auto') (line 305846)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), -1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), -1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AL PDI; 5 ('RTL') (line 305846)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0608, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO EN B; 4 ('RTL') (line 305847)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0030, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO EN S; 4 ('RTL') (line 305848)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0030, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO EN WS; 4 ('RTL') (line 305849)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0030, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO EN LRI; 4 ('RTL') (line 305850)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0030, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_035)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN LRO EN RLI; 4 ('RTL') (line 305851)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0030, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO EN FSI; 4 ('RTL') (line 305852)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0030, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO EN PDI; 4 ('RTL') (line 305853)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0030, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ES B; 4 ('RTL') (line 305854)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002B, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ES S; 4 ('RTL') (line 305855)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002B, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ES WS; 4 ('RTL') (line 305856)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002B, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ES LRI; 4 ('RTL') (line 305857)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002B, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ES RLI; 4 ('RTL') (line 305858)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002B, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ES FSI; 4 ('RTL') (line 305859)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002B, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ES PDI; 4 ('RTL') (line 305860)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002B, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_036)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN LRO ET B; 4 ('RTL') (line 305861)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0023, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ET S; 4 ('RTL') (line 305862)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0023, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ET WS; 4 ('RTL') (line 305863)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0023, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ET LRI; 4 ('RTL') (line 305864)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0023, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ET RLI; 4 ('RTL') (line 305865)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0023, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ET FSI; 4 ('RTL') (line 305866)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0023, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ET PDI; 4 ('RTL') (line 305867)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0023, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AN B; 4 ('RTL') (line 305868)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0660, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AN S; 4 ('RTL') (line 305869)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0660, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AN WS; 4 ('RTL') (line 305870)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0660, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_037)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN LRO AN LRI; 4 ('RTL') (line 305871)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0660, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AN RLI; 4 ('RTL') (line 305872)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0660, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AN FSI; 4 ('RTL') (line 305873)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0660, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO AN PDI; 4 ('RTL') (line 305874)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0660, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO CS B; 4 ('RTL') (line 305875)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002C, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO CS S; 4 ('RTL') (line 305876)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002C, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO CS WS; 4 ('RTL') (line 305877)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002C, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO CS LRI; 4 ('RTL') (line 305878)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002C, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO CS RLI; 4 ('RTL') (line 305879)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002C, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO CS FSI; 4 ('RTL') (line 305880)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002C, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_038)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN LRO CS PDI; 4 ('RTL') (line 305881)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x002C, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO NSM B; 4 ('RTL') (line 305882)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0300, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO NSM S; 4 ('RTL') (line 305883)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0300, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO NSM WS; 4 ('RTL') (line 305884)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0300, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO NSM LRI; 4 ('RTL') (line 305885)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0300, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO NSM RLI; 4 ('RTL') (line 305886)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0300, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO NSM FSI; 4 ('RTL') (line 305887)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0300, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO NSM PDI; 4 ('RTL') (line 305888)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0300, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ON B; 4 ('RTL') (line 305889)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0021, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ON S; 4 ('RTL') (line 305890)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0021, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_039)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN LRO ON WS; 4 ('RTL') (line 305891)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0021, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ON LRI; 4 ('RTL') (line 305892)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0021, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ON RLI; 4 ('RTL') (line 305893)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0021, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ON FSI; 4 ('RTL') (line 305894)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0021, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN LRO ON PDI; 4 ('RTL') (line 305895)
        std::vector<uint32_t> const cps = { 0x0660, 0x202D, 0x0021, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE L R; 3 ('auto') (line 305896)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE L R; 3 ('LTR') (line 305896)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0041, 0x05BE };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE L AL; 3 ('auto') (line 305897)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE L AL; 3 ('LTR') (line 305897)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0041, 0x0608 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE L ES; 3 ('auto') (line 305898)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE L ES; 3 ('LTR') (line 305898)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0041, 0x002B };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE L ET; 3 ('auto') (line 305899)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE L ET; 3 ('LTR') (line 305899)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0041, 0x0023 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE L CS; 3 ('auto') (line 305900)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE L CS; 3 ('LTR') (line 305900)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0041, 0x002C };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_040)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN RLE L ON; 3 ('auto') (line 305901)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE L ON; 3 ('LTR') (line 305901)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0041, 0x0021 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE EN R; 2 ('LTR') (line 305902)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0030, 0x05BE };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE EN AL; 2 ('LTR') (line 305903)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0030, 0x0608 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE EN ES; 3 ('auto') (line 305904)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE EN ES; 3 ('LTR') (line 305904)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0030, 0x002B };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE EN CS; 3 ('auto') (line 305905)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE EN CS; 3 ('LTR') (line 305905)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0030, 0x002C };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE EN ON; 3 ('auto') (line 305906)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE EN ON; 3 ('LTR') (line 305906)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0030, 0x0021 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE AN R; 2 ('LTR') (line 305907)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0660, 0x05BE };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE AN AL; 2 ('LTR') (line 305908)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0660, 0x0608 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE AN ES; 3 ('auto') (line 305909)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE AN ES; 3 ('LTR') (line 305909)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0660, 0x002B };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE AN ET; 3 ('auto') (line 305910)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE AN ET; 3 ('LTR') (line 305910)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0660, 0x0023 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_041)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN RLE AN CS; 3 ('auto') (line 305911)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE AN CS; 3 ('LTR') (line 305911)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0660, 0x002C };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE AN ON; 3 ('auto') (line 305912)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN RLE AN ON; 3 ('LTR') (line 305912)
        std::vector<uint32_t> const cps = { 0x0660, 0x202B, 0x0660, 0x0021 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF L R; 4 ('RTL') (line 305913)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF L AL; 4 ('RTL') (line 305914)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF L ES; 4 ('RTL') (line 305915)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF L ET; 4 ('RTL') (line 305916)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF L CS; 4 ('RTL') (line 305917)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF L B; 4 ('RTL') (line 305918)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF L S; 4 ('RTL') (line 305919)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF L WS; 4 ('RTL') (line 305920)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_042)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN PDF L ON; 4 ('RTL') (line 305921)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF L LRI; 4 ('RTL') (line 305922)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF L RLI; 4 ('RTL') (line 305923)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF L FSI; 4 ('RTL') (line 305924)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF L PDI; 4 ('RTL') (line 305925)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0041, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN R; 5 ('auto') (line 305926)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN R; 5 ('RTL') (line 305926)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN AL; 5 ('auto') (line 305927)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN AL; 5 ('RTL') (line 305927)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN ES; 4 ('RTL') (line 305928)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN CS; 4 ('RTL') (line 305929)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN B; 4 ('RTL') (line 305930)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_043)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN PDF EN S; 4 ('RTL') (line 305931)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN WS; 4 ('RTL') (line 305932)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN ON; 4 ('RTL') (line 305933)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN LRI; 4 ('RTL') (line 305934)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN RLI; 4 ('RTL') (line 305935)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN FSI; 4 ('RTL') (line 305936)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF EN PDI; 4 ('RTL') (line 305937)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0030, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN R; 7 ('auto') (line 305938)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN R; 7 ('LTR') (line 305938)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 0x05BE };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN R; 7 ('RTL') (line 305938)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN AL; 7 ('auto') (line 305939)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN AL; 7 ('LTR') (line 305939)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 0x0608 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN AL; 7 ('RTL') (line 305939)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN ES; 4 ('RTL') (line 305940)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_044)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN PDF AN ET; 4 ('RTL') (line 305941)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN CS; 4 ('RTL') (line 305942)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN B; 4 ('RTL') (line 305943)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN S; 4 ('RTL') (line 305944)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN WS; 4 ('RTL') (line 305945)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN ON; 4 ('RTL') (line 305946)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN LRI; 4 ('RTL') (line 305947)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN RLI; 4 ('RTL') (line 305948)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN FSI; 4 ('RTL') (line 305949)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF AN PDI; 4 ('RTL') (line 305950)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0660, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_045)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN PDF NSM R; 7 ('auto') (line 305951)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM R; 7 ('LTR') (line 305951)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 0x05BE };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM R; 7 ('RTL') (line 305951)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM AL; 7 ('auto') (line 305952)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM AL; 7 ('LTR') (line 305952)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 0x0608 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM AL; 7 ('RTL') (line 305952)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM ES; 4 ('RTL') (line 305953)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM ET; 4 ('RTL') (line 305954)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM CS; 4 ('RTL') (line 305955)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM B; 4 ('RTL') (line 305956)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM S; 4 ('RTL') (line 305957)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM WS; 4 ('RTL') (line 305958)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM ON; 4 ('RTL') (line 305959)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM LRI; 4 ('RTL') (line 305960)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_046)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN PDF NSM RLI; 4 ('RTL') (line 305961)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM FSI; 4 ('RTL') (line 305962)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN PDF NSM PDI; 4 ('RTL') (line 305963)
        std::vector<uint32_t> const cps = { 0x0660, 0x202C, 0x0300, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN L R; 4 ('RTL') (line 305964)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN L AL; 4 ('RTL') (line 305965)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN L ES; 4 ('RTL') (line 305966)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN L ET; 4 ('RTL') (line 305967)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN L CS; 4 ('RTL') (line 305968)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN L B; 4 ('RTL') (line 305969)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN L S; 4 ('RTL') (line 305970)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_047)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN BN L WS; 4 ('RTL') (line 305971)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN L ON; 4 ('RTL') (line 305972)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN L LRI; 4 ('RTL') (line 305973)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN L RLI; 4 ('RTL') (line 305974)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN L FSI; 4 ('RTL') (line 305975)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN L PDI; 4 ('RTL') (line 305976)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0041, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN R; 5 ('auto') (line 305977)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN R; 5 ('RTL') (line 305977)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN AL; 5 ('auto') (line 305978)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN AL; 5 ('RTL') (line 305978)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN ES; 4 ('RTL') (line 305979)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN CS; 4 ('RTL') (line 305980)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_048)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN BN EN B; 4 ('RTL') (line 305981)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN S; 4 ('RTL') (line 305982)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN WS; 4 ('RTL') (line 305983)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN ON; 4 ('RTL') (line 305984)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN LRI; 4 ('RTL') (line 305985)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN RLI; 4 ('RTL') (line 305986)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN FSI; 4 ('RTL') (line 305987)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN EN PDI; 4 ('RTL') (line 305988)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0030, 0x2069 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN R; 7 ('auto') (line 305989)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN R; 7 ('LTR') (line 305989)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 0x05BE };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN R; 7 ('RTL') (line 305989)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // <NAME>; 7 ('auto') (line 305990)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, -1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN AL; 7 ('LTR') (line 305990)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 0x0608 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 0);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 0);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 0)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // <NAME>; 7 ('RTL') (line 305990)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], 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_611_049)
{

    expected_levels = { 2, -1, 2, 1 };
    expected_reordered_indices = { 3, 0, 2 };

    {
        // AN BN AN ES; 4 ('RTL') (line 305991)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN ET; 4 ('RTL') (line 305992)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN CS; 4 ('RTL') (line 305993)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN B; 4 ('RTL') (line 305994)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 0x2029 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN S; 4 ('RTL') (line 305995)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 0x0009 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN WS; 4 ('RTL') (line 305996)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 0x0020 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN ON; 4 ('RTL') (line 305997)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 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], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN LRI; 4 ('RTL') (line 305998)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 0x2066 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN RLI; 4 ('RTL') (line 305999)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 0x2067 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

    {
        // AN BN AN FSI; 4 ('RTL') (line 306000)
        std::vector<uint32_t> const cps = { 0x0660, 0x00AD, 0x0660, 0x2068 };
        std::vector<int> const levels =
            bidi_levels(cps.begin(), cps.end(), 1);
        int i = 0;
        for (int l : expected_levels) {
            if (0 <= l) {
                EXPECT_EQ(levels[i], l) << "i=" << i;
                ++i;
            }
        }
        EXPECT_EQ((int)levels.size(), i);

        std::vector<int> const reordered =
            bidi_reordered_indices(cps.begin(), cps.end(), 1);
        i = 0;
        for (int idx : expected_reordered_indices) {
            // Skip FSI, LRI, RLI, and PDI.
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }

        std::vector<int> reordered_2;
        for (auto subrange :
            boost::text::bidirectional_subranges(cps, 1)) {
            for (auto cp : subrange) {
                reordered_2.push_back(cp);
            }
        }
        i = 0;
        for (int idx : expected_reordered_indices) {
            if (cps[idx] < 0x2066 || 0x2069 < cps[idx]) {
                EXPECT_EQ(reordered_2[i], cps[idx])
                    << std::hex
                    << " 0x" << reordered_2[i]
                    << " 0x" << cps[idx]
                    << std::dec << " i=" << i;
            }
            ++i;
        }
        EXPECT_EQ(i, (int)reordered_2.size());
    }

}
