# PRE: if
#
string test_string
string dummy_string
uint32 test_integer

test_integer := '123456789'

# Non matching on attribute ref
if !(User-Name =~ /^([0-9])_([0-9])?_([0-9]*)_([0-9]+)_([^_])_(6)_([7-8])%{test_string}/) {
	test_fail
}

# Matching on xlat expanded value
if !("%{User-Name}" =~ /^([0-9])_([0-9])?_([0-9]*)_([0-9]+)_([^_])_(6)_([7-8])%{test_string}/) {
	test_fail
}

# Matching on attribute ref with capture groups
if (User-Name =~ /^([0-9])_([0-9])?_([0-9]*)_([0-9]+)_([^_])_(6)_([7-8])%{test_string}/) {
	# Test all the capture groups
	reply.User-Name := "%{7}_%{6}_%{5}_%{4}_%{3}_%{2}_%{1}_%{0}"
}
else {
	test_fail
}

# Checking capture groups are cleared out correctly
if (User-Name =~ /^([0-9])_%{test_string}/) {
	if (!("%{0}%{1}%{2}%{3}%{4}%{5}%{6}%{7}" == '1_1')) {
		test_fail
	}
}
else {
	test_fail
}

# Checking capture groups are cleared out correctly when there are no matches
if (User-Name =~ /^.%{test_string}/) {
	if (!("%{0}%{1}%{2}%{3}%{4}%{5}%{6}%{7}" == '1')) {
		test_fail
	}
}
else {
	test_fail
}

# Checking full capture group range
if ('a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z_A_B_C_D_E_F' =~ /^(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)$/) {
	if (!("%{0}%{1}%{2}%{3}%{4}%{5}%{6}%{7}%{8}%{9}%{10}%{11}%{12}%{13}%{14}%{15}%{16}%{17}%{18}%{19}%{20}%{21}%{22}%{23}%{24}%{25}%{26}%{27}%{28}%{29}%{30}%{31}%{32}" == 'a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z_A_B_C_D_E_FabcdefghijklmnopqrstuvwxyzABCDEF')) {
		test_fail
	}
}
else {
	test_fail
}

# Checking full capture group overrun
if ('a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z_A_B_C_D_E_F_G' =~ /^(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)_(.)$/) {
	if (!("%{0}%{1}%{2}%{3}%{4}%{5}%{6}%{7}%{8}%{9}%{10}%{11}%{12}%{13}%{14}%{15}%{16}%{17}%{18}%{19}%{20}%{21}%{22}%{23}%{24}%{25}%{26}%{27}%{28}%{29}%{30}%{31}%{32}" == 'a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z_A_B_C_D_E_F_GabcdefghijklmnopqrstuvwxyzABCDEF')) {
		test_fail
	}
}
else {
	test_fail
}

# uncompiled - ref - insensitive
if !(Calling-Station-Id =~ /:roamyroam%{test_string}$/i) {
	test_fail
}

# uncompiled - expansion - insensitive
if !(Calling-Station-Id =~ /:roamyroam%{test_string}$/i) {
	test_fail
}

# uncompiled - enum - ref - insensitive
if !(Service-Type =~ /^framed-user%{test_string}$/i) {
	test_fail
}

# uncompiled - enum - expansion - insensitive
if !("%{Service-Type}" =~ /^framed-user%{test_string}$/i) {
	test_fail
}

# uncompiled - enum - ref
if (Service-Type =~ /^framed-user%{test_string}$/) {
	test_fail
}

# uncompiled - integer - ref
if !(test_integer =~ /%{test_integer}/) {
	test_fail
}

test_string := "foo\nbar"

# uncompiled - ref - multiline
if !(test_string =~ /^foo$%{dummy_string}/m) {
	test_fail
}

# uncompiled - ref - non-multiline
if (test_string =~ /^foo$%{dummy_string}/) {
	test_fail
}

# uncompiled - ref - non-multiline
if !(test_string =~ /^foo\nbar%{dummy_string}$/) {
	test_fail
}

# uncompiled - ref - multiline
if !(test_string =~ /^bar%{dummy_string}$/m) {
	test_fail
}

# uncompiled - ref - multiline - sensitive
if (test_string =~ /^BAR%{dummy_string}$/m) {
	test_fail
}

# uncompiled - ref - multiline - insensitive
if !(test_string =~ /^BAR%{dummy_string}$/mi) {
	test_fail
}

# uncompiled - ref - multiline - insensitive (flag order reversed)
if !(test_string =~ /^BAR%{dummy_string}$/im) {
	test_fail
}
