void TextProcessorTester::testContextSubtable()
{
    Builder builder;

    vector<LookupSubtable *> simpleReferral = {
        &builder.createSingleSubst({ 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 10)
    };
    vector<LookupSubtable *> complexReferral = {
        &builder.createSingleSubst({ 1, 2, 3, 4, 5, 6 }, 1),
        &builder.createMultipleSubst({ {2, { 4, 5, 6 }} }),
        &builder.createLigatureSubst({ {{ 1, 4 }, 10}, {{ 6, 4 }, 20} })
    };

    /* Test the format 1. */
    {
        /* Test with unmatching first input glyph. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 2, 3 }, { {1, 1} } }
                         }),
                         { 0, 2, 3 }, { 0, 2, 3 }, simpleReferral);
        /* Test with unmatching middle input glyph. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 2, 3 }, { {1, 1} } }
                         }),
                         { 1, 0, 3 }, { 1, 0, 3 }, simpleReferral);
        /* Test with unmatching last input glyph. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 2, 3 }, { {1, 1} } }
                         }),
                         { 1, 2, 0 }, { 1, 2, 0 }, simpleReferral);
        /* Test by providing single input glyph. */
        testSubstitution(builder.createContext({
                            rule_context { { 1 }, { {0, 1} } }
                         }),
                         { 1 }, { 11 }, simpleReferral);
        /* Test by providing same input glyph sequence. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 1, 1 }, { {1, 1} } }
                         }),
                         { 1, 1, 1 }, { 1, 11, 1 }, simpleReferral);
        /* Test by applying lookup on first input glyph. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 2, 3 }, { {0, 1} } }
                         }),
                         { 1, 2, 3 }, { 11, 2, 3 }, simpleReferral);
        /* Test by applying lookup on middle input glyph. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 2, 3 }, { {1, 1} } }
                         }),
                         { 1, 2, 3 }, { 1, 12, 3 }, simpleReferral);
        /* Test by applying lookup on last input glyph. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 2, 3 }, { {2, 1} } }
                         }),
                         { 1, 2, 3 }, { 1, 2, 13 }, simpleReferral);
        /* Test by applying lookup on each input glyph. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 2, 3 }, { {0, 1}, {1, 1}, {2, 1} } }
                         }),
                         { 1, 2, 3 }, { 11, 12, 13 }, simpleReferral);
        /* Test by letting middle rule match in a single rule set. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 2, 3 }, { {1, 1} } },
                            rule_context { { 1, 2, 4 }, { {1, 1} } },
                            rule_context { { 1, 2, 5 }, { {1, 1} } },
                         }),
                         { 1, 2, 4 }, { 1, 12, 4 }, simpleReferral);
        /* Test by letting last rule match in a single rule set. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 2, 3 }, { {1, 1} } },
                            rule_context { { 1, 2, 4 }, { {1, 1} } },
                            rule_context { { 1, 2, 5 }, { {1, 1} } },
                         }),
                         { 1, 2, 5 }, { 1, 12, 5 }, simpleReferral);
        /* Test by letting middle rule match in multiple rule sets. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 2, 3 }, { {1, 1} } },
                            rule_context { { 4, 5, 6 }, { {1, 1} } },
                            rule_context { { 7, 8, 9 }, { {1, 1} } },
                         }),
                         { 4, 5, 6 }, { 4, 15, 6 }, simpleReferral);
        /* Test by letting last rule match in multiple rule sets. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 2, 3 }, { {1, 1} } },
                            rule_context { { 4, 5, 6 }, { {1, 1} } },
                            rule_context { { 7, 8, 9 }, { {1, 1} } },
                         }),
                         { 7, 8, 9 }, { 7, 18, 9 }, simpleReferral);
        /* Test by applying complex lookups on input glyphs. */
        testSubstitution(builder.createContext({
                            rule_context { { 1, 2, 3 }, { {2, 1}, {1, 2}, {3, 3}, {0, 3}, {1, 1} } }
                         }),
                         {  1, 2, 3 }, { 10, 6, 20 }, complexReferral);
    }

    /* Test the format 2. */
    {
        ClassDefTable &classDef = builder.createClassDef({
            class_range { 1, 3, 1 },
            class_range { 4, 6, 2 },
            class_range { 7, 9, 3 },
        });

        /* Test with unmatching first input glyph. */
        testSubstitution(builder.createContext({ 1 }, classDef, {
                            rule_context { { 1, 1, 1 }, { {1, 1} } }
                         }),
                         { 0, 2, 3 }, { 0, 2, 3 }, simpleReferral);
        /* Test with unmatching middle input glyph. */
        testSubstitution(builder.createContext({ 1 }, classDef, {
                            rule_context { { 1, 1, 1 }, { {1, 1} } }
                         }),
                         { 1, 0, 3 }, { 1, 0, 3 }, simpleReferral);
        /* Test with unmatching last input glyph. */
        testSubstitution(builder.createContext({ 1 }, classDef, {
                            rule_context { { 1, 1, 1 }, { {1, 1} } }
                         }),
                         { 1, 2, 0 }, { 1, 2, 0 }, simpleReferral);
        /* Test by providing single input glyph. */
        testSubstitution(builder.createContext({ 1 }, classDef, {
                            rule_context { { 1 }, { {0, 1} } }
                         }),
                         { 1 }, { 11 }, simpleReferral);
        /* Test by applying lookup on first input glyph. */
        testSubstitution(builder.createContext({ 1 }, classDef, {
                            rule_context { { 1, 1, 1 }, { {0, 1} } }
                         }),
                         { 1, 2, 3 }, { 11, 2, 3 }, simpleReferral);
        /* Test by applying lookup on middle input glyph. */
        testSubstitution(builder.createContext({ 1 }, classDef, {
                            rule_context { { 1, 1, 1 }, { {1, 1} } }
                         }),
                         { 1, 2, 3 }, { 1, 12, 3 }, simpleReferral);
        /* Test by applying lookup on last input glyph. */
        testSubstitution(builder.createContext({ 1 }, classDef, {
                            rule_context { { 1, 1, 1 }, { {2, 1} } }
                         }),
                         { 1, 2, 3 }, { 1, 2, 13 }, simpleReferral);
        /* Test by applying lookup on each input glyph. */
        testSubstitution(builder.createContext({ 1 }, classDef, {
                            rule_context { { 1, 1, 1 }, { {0, 1}, {1, 1}, {2, 1} } }
                         }),
                         { 1, 2, 3 }, { 11, 12, 13 }, simpleReferral);
        /* Test by letting middle rule match in a single rule set. */
        testSubstitution(builder.createContext({ 1 }, classDef, {
                            rule_context { { 1, 1, 1 }, { {1, 1} } },
                            rule_context { { 1, 1, 2 }, { {1, 1} } },
                            rule_context { { 1, 1, 3 }, { {1, 1} } },
                         }),
                         { 1, 2, 5 }, { 1, 12, 5 }, simpleReferral);
        /* Test by letting last rule match in a single rule set. */
        testSubstitution(builder.createContext({ 1 }, classDef, {
                            rule_context { { 1, 1, 1 }, { {1, 1} } },
                            rule_context { { 1, 1, 2 }, { {1, 1} } },
                            rule_context { { 1, 1, 3 }, { {1, 1} } },
                         }),
                         { 1, 2, 8 }, { 1, 12, 8 }, simpleReferral);
        /* Test by letting middle rule match in multiple rule sets. */
        testSubstitution(builder.createContext({ 4 }, classDef, {
                            rule_context { { 1, 1, 1 }, { {1, 1} } },
                            rule_context { { 2, 2, 2 }, { {1, 1} } },
                            rule_context { { 3, 3, 3 }, { {1, 1} } },
                         }),
                         { 4, 5, 6 }, { 4, 15, 6 }, simpleReferral);
        /* Test by letting last rule match in multiple rule sets. */
        testSubstitution(builder.createContext({ 7 }, classDef, {
                            rule_context { { 1, 1, 1 }, { {1, 1} } },
                            rule_context { { 2, 2, 2 }, { {1, 1} } },
                            rule_context { { 3, 3, 3 }, { {1, 1} } },
                         }),
                         { 7, 8, 9 }, { 7, 18, 9 }, simpleReferral);
        /* Test by applying complex lookups on input glyphs. */
        testSubstitution(builder.createContext({ 1 }, classDef, {
                            rule_context { { 1, 1, 1 }, { {2, 1}, {1, 2}, {3, 3}, {0, 3}, {1, 1} } }
                         }),
                         {  1, 2, 3 }, { 10, 6, 20 }, complexReferral);
    }

    /* Test the format 3. */
    {
        /* Test with unmatching first input glyph. */
        testSubstitution(builder.createContext({ { 1 }, { 2 }, { 3 } }, { {1, 1} }),
                         { 0, 2, 3 }, { 0, 2, 3 }, simpleReferral);
        /* Test with unmatching middle input glyph. */
        testSubstitution(builder.createContext({ { 1 }, { 2 }, { 3 } }, { {1, 1} }),
                         { 1, 0, 3 }, { 1, 0, 3 }, simpleReferral);
        /* Test with unmatching last input glyph. */
        testSubstitution(builder.createContext({ { 1 }, { 2 }, { 3 } }, { {1, 1} }),
                         { 1, 2, 0 }, { 1, 2, 0 }, simpleReferral);
        /* Test by providing single input glyph. */
        testSubstitution(builder.createContext({ { 1 } }, { {0, 1} }),
                         { 1 }, { 11 }, simpleReferral);
        /* Test by providing same input glyph sequence. */
        testSubstitution(builder.createContext({ { 1 }, { 1 }, { 1 } }, { {1, 1} }),
                         { 1, 1, 1 }, { 1, 11, 1 }, simpleReferral);
        /* Test by applying lookup on first input glyph. */
        testSubstitution(builder.createContext({ { 1 }, { 2 }, { 3 } }, { {0, 1} }),
                         { 1, 2, 3 }, { 11, 2, 3 }, simpleReferral);
        /* Test by applying lookup on middle input glyph. */
        testSubstitution(builder.createContext({ { 1 }, { 2 }, { 3 } }, { {1, 1} }),
                         { 1, 2, 3 }, { 1, 12, 3 }, simpleReferral);
        /* Test by applying lookup on last input glyph. */
        testSubstitution(builder.createContext({ { 1 }, { 2 }, { 3 } }, { {2, 1} }),
                         { 1, 2, 3 }, { 1, 2, 13 }, simpleReferral);
        /* Test by applying lookup on each input glyph. */
        testSubstitution(builder.createContext({ { 1 }, { 2 }, { 3 } }, { {0, 1}, {1, 1}, {2, 1} }),
                         { 1, 2, 3 }, { 11, 12, 13 }, simpleReferral);
        /* Test by applying complex lookups on input glyphs. */
        testSubstitution(builder.createContext({ { 1 }, { 2 }, { 3 } }, { {2, 1}, {1, 2}, {3, 3}, {0, 3}, {1, 1} }),
                         {  1, 2, 3 }, { 10, 6, 20 }, complexReferral);
    }
}