From 98803c070fb10054ee8371dea58b27f73f538979 Mon Sep 17 00:00:00 2001 From: Chris Sangwin Date: Tue, 26 Nov 2024 10:19:00 +0000 Subject: [PATCH] Issue #1243: reinstate nofunctions validator code. --- stack/maxima/contrib/validators.mac | 9 +++++++++ stack/maxima/contrib/validators_test.mac | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/stack/maxima/contrib/validators.mac b/stack/maxima/contrib/validators.mac index 14472483a6b..8c94f0807d5 100644 --- a/stack/maxima/contrib/validators.mac +++ b/stack/maxima/contrib/validators.mac @@ -31,6 +31,15 @@ validate_underscore(ex) := if is(sposition("_", string(ex)) = false) then "" /* Add in unit-test cases using STACK's s_test_case function. At least two please! */ /* Place test cases in validators_test.mac */ +/* The student may not use a user-defined function, or arrays, anywhere in their input. */ +validate_nofunctions(ex):= block([op1,opp], + if atom(ex) then return(""), + op1:ev(op(ex)), + opp:apply(properties, [op1]), + if ev(emptyp(opp) or is(opp=[noun]),simp) then return(sconcat("User-defined functions are not permitted in this input. In your answer ", stack_disp(op1, "i"), " appears to be used as a function. ")), + apply(sconcat, map(validate_nofunctions, args(ex))) +); + /* The student may only use single-character variable names in their answer. */ /* This is intended for use when Insert Stars is turned off, but we still want to indicate to students that they may have forgotten a star */ validate_all_one_letter_variables(ex) := if not(is(ev(lmax(map(lambda([ex2],slength(string(ex2))),listofvars(ex))),simp)>1)) then "" diff --git a/stack/maxima/contrib/validators_test.mac b/stack/maxima/contrib/validators_test.mac index 2c700dbc082..3a59409ad80 100644 --- a/stack/maxima/contrib/validators_test.mac +++ b/stack/maxima/contrib/validators_test.mac @@ -23,6 +23,14 @@ /* */ /****************************************************************/ + +s_test_case(validate_nofunctions(1+a1), ""); +s_test_case(validate_nofunctions(sin(n*x)), ""); +s_test_case(validate_nofunctions(-b#pm#sqrt(b^2-4*a*c)), ""); +s_test_case(validate_nofunctions(x(2)), "User-defined functions are not permitted in this input. In your answer \\(x\\) appears to be used as a function. "); +s_test_case(validate_nofunctions(3*x(t)^2), "User-defined functions are not permitted in this input. In your answer \\(x\\) appears to be used as a function. "); +s_test_case(validate_nofunctions(1+f(x+1)), "User-defined functions are not permitted in this input. In your answer \\(f\\) appears to be used as a function. "); +s_test_case(validate_nofunctions(x(2)*y(3)), "User-defined functions are not permitted in this input. In your answer \\(x\\) appears to be used as a function. User-defined functions are not permitted in this input. In your answer \\(y\\) appears to be used as a function. "); s_test_case(validate_underscore(1+a1), ""); s_test_case(validate_underscore(1+a_1), "Underscore characters are not permitted in this input.");