Add trim function to ignore the whitespace character.

Add a trim key to mark whether the answer should be trimmed.
Add a judgement to determine whether the trim should work.
This commit is contained in:
dioxide 2025-06-15 17:16:39 +08:00 committed by fly6516
parent 5e89d67190
commit 3a68b9bc9b
6 changed files with 342 additions and 6 deletions

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Problem" ADD COLUMN "trim" BOOLEAN NOT NULL DEFAULT false;

View File

@ -0,0 +1,164 @@
/*
Warnings:
- The primary key for the `DockerConfig` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The primary key for the `LanguageServerConfig` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `createdAt` on the `Problem` table. All the data in the column will be lost.
- You are about to drop the column `isPublished` on the `Problem` table. All the data in the column will be lost.
- You are about to drop the column `updatedAt` on the `Problem` table. All the data in the column will be lost.
- You are about to drop the column `content` on the `Submission` table. All the data in the column will be lost.
- You are about to drop the column `timeUsage` on the `Submission` table. All the data in the column will be lost.
- The primary key for the `Template` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `content` on the `Template` table. All the data in the column will be lost.
- You are about to drop the column `createdAt` on the `Testcase` table. All the data in the column will be lost.
- You are about to drop the column `updatedAt` on the `Testcase` table. All the data in the column will be lost.
- You are about to drop the column `timeUsage` on the `TestcaseResult` table. All the data in the column will be lost.
- You are about to drop the `ProblemLocalization` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `TestcaseInput` table. If the table is not empty, all the data it contains will be lost.
- A unique constraint covering the columns `[language]` on the table `DockerConfig` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[language]` on the table `LanguageServerConfig` will be added. If there are existing duplicate values, this will fail.
- Changed the type of `language` on the `DockerConfig` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `language` on the `LanguageServerConfig` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `protocol` on the `LanguageServerConfig` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Added the required column `description` to the `Problem` table without a default value. This is not possible if the table is not empty.
- Added the required column `solution` to the `Problem` table without a default value. This is not possible if the table is not empty.
- Added the required column `title` to the `Problem` table without a default value. This is not possible if the table is not empty.
- Made the column `userId` on table `Problem` required. This step will fail if there are existing NULL values in that column.
- Added the required column `code` to the `Submission` table without a default value. This is not possible if the table is not empty.
- Changed the type of `language` on the `Submission` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Added the required column `template` to the `Template` table without a default value. This is not possible if the table is not empty.
- Changed the type of `language` on the `Template` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Made the column `output` on table `TestcaseResult` required. This step will fail if there are existing NULL values in that column.
*/
-- CreateEnum
CREATE TYPE "EditorLanguage" AS ENUM ('c', 'cpp');
-- CreateEnum
CREATE TYPE "LanguageServerProtocol" AS ENUM ('ws', 'wss');
-- DropForeignKey
ALTER TABLE "Problem" DROP CONSTRAINT "Problem_userId_fkey";
-- DropForeignKey
ALTER TABLE "ProblemLocalization" DROP CONSTRAINT "ProblemLocalization_problemId_fkey";
-- DropForeignKey
ALTER TABLE "TestcaseInput" DROP CONSTRAINT "TestcaseInput_testcaseId_fkey";
-- AlterTable
ALTER TABLE "DockerConfig" DROP CONSTRAINT "DockerConfig_pkey",
DROP COLUMN "language",
ADD COLUMN "language" "EditorLanguage" NOT NULL,
ALTER COLUMN "compileOutputLimit" DROP DEFAULT,
ALTER COLUMN "runOutputLimit" DROP DEFAULT;
-- AlterTable
ALTER TABLE "LanguageServerConfig" DROP CONSTRAINT "LanguageServerConfig_pkey",
DROP COLUMN "language",
ADD COLUMN "language" "EditorLanguage" NOT NULL,
DROP COLUMN "protocol",
ADD COLUMN "protocol" "LanguageServerProtocol" NOT NULL;
-- AlterTable
ALTER TABLE "Problem" DROP COLUMN "createdAt",
DROP COLUMN "isPublished",
DROP COLUMN "updatedAt",
ADD COLUMN "description" TEXT NOT NULL,
ADD COLUMN "published" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN "solution" TEXT NOT NULL,
ADD COLUMN "title" TEXT NOT NULL,
ALTER COLUMN "memoryLimit" SET DEFAULT 128,
ALTER COLUMN "userId" SET NOT NULL;
-- AlterTable
ALTER TABLE "Submission" DROP COLUMN "content",
DROP COLUMN "timeUsage",
ADD COLUMN "code" TEXT NOT NULL,
ADD COLUMN "executionTime" INTEGER,
DROP COLUMN "language",
ADD COLUMN "language" "EditorLanguage" NOT NULL;
-- AlterTable
ALTER TABLE "Template" DROP CONSTRAINT "Template_pkey",
DROP COLUMN "content",
ADD COLUMN "template" TEXT NOT NULL,
DROP COLUMN "language",
ADD COLUMN "language" "EditorLanguage" NOT NULL,
ADD CONSTRAINT "Template_pkey" PRIMARY KEY ("problemId", "language");
-- AlterTable
ALTER TABLE "Testcase" DROP COLUMN "createdAt",
DROP COLUMN "updatedAt";
-- AlterTable
ALTER TABLE "TestcaseResult" DROP COLUMN "timeUsage",
ADD COLUMN "executionTime" INTEGER,
ALTER COLUMN "output" SET NOT NULL;
-- AlterTable
ALTER TABLE "User" ALTER COLUMN "email" DROP NOT NULL;
-- DropTable
DROP TABLE "ProblemLocalization";
-- DropTable
DROP TABLE "TestcaseInput";
-- DropEnum
DROP TYPE "Language";
-- DropEnum
DROP TYPE "Locale";
-- DropEnum
DROP TYPE "ProblemContentType";
-- DropEnum
DROP TYPE "Protocol";
-- CreateTable
CREATE TABLE "EditorLanguageConfig" (
"language" "EditorLanguage" NOT NULL,
"label" TEXT NOT NULL,
"fileName" TEXT NOT NULL,
"fileExtension" TEXT NOT NULL
);
-- CreateTable
CREATE TABLE "TestcaseData" (
"id" TEXT NOT NULL,
"index" INTEGER NOT NULL,
"label" TEXT NOT NULL,
"value" TEXT NOT NULL,
"testcaseId" TEXT NOT NULL,
CONSTRAINT "TestcaseData_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "EditorLanguageConfig_language_key" ON "EditorLanguageConfig"("language");
-- CreateIndex
CREATE UNIQUE INDEX "DockerConfig_language_key" ON "DockerConfig"("language");
-- CreateIndex
CREATE UNIQUE INDEX "LanguageServerConfig_language_key" ON "LanguageServerConfig"("language");
-- CreateIndex
CREATE INDEX "Problem_userId_idx" ON "Problem"("userId");
-- CreateIndex
CREATE INDEX "Problem_difficulty_idx" ON "Problem"("difficulty");
-- AddForeignKey
ALTER TABLE "Problem" ADD CONSTRAINT "Problem_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "LanguageServerConfig" ADD CONSTRAINT "LanguageServerConfig_language_fkey" FOREIGN KEY ("language") REFERENCES "EditorLanguageConfig"("language") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "DockerConfig" ADD CONSTRAINT "DockerConfig_language_fkey" FOREIGN KEY ("language") REFERENCES "EditorLanguageConfig"("language") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "TestcaseData" ADD CONSTRAINT "TestcaseData_testcaseId_fkey" FOREIGN KEY ("testcaseId") REFERENCES "Testcase"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -0,0 +1,156 @@
/*
Warnings:
- You are about to drop the column `description` on the `Problem` table. All the data in the column will be lost.
- You are about to drop the column `published` on the `Problem` table. All the data in the column will be lost.
- You are about to drop the column `solution` on the `Problem` table. All the data in the column will be lost.
- You are about to drop the column `title` on the `Problem` table. All the data in the column will be lost.
- You are about to drop the column `code` on the `Submission` table. All the data in the column will be lost.
- You are about to drop the column `executionTime` on the `Submission` table. All the data in the column will be lost.
- The primary key for the `Template` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `template` on the `Template` table. All the data in the column will be lost.
- You are about to drop the column `executionTime` on the `TestcaseResult` table. All the data in the column will be lost.
- You are about to drop the `EditorLanguageConfig` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `TestcaseData` table. If the table is not empty, all the data it contains will be lost.
- Changed the type of `language` on the `DockerConfig` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `language` on the `LanguageServerConfig` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `protocol` on the `LanguageServerConfig` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Added the required column `updatedAt` to the `Problem` table without a default value. This is not possible if the table is not empty.
- Added the required column `content` to the `Submission` table without a default value. This is not possible if the table is not empty.
- Changed the type of `language` on the `Submission` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Added the required column `content` to the `Template` table without a default value. This is not possible if the table is not empty.
- Changed the type of `language` on the `Template` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Added the required column `updatedAt` to the `Testcase` table without a default value. This is not possible if the table is not empty.
- Made the column `email` on table `User` required. This step will fail if there are existing NULL values in that column.
*/
-- CreateEnum
CREATE TYPE "Locale" AS ENUM ('en', 'zh');
-- CreateEnum
CREATE TYPE "Language" AS ENUM ('c', 'cpp');
-- CreateEnum
CREATE TYPE "Protocol" AS ENUM ('ws', 'wss');
-- CreateEnum
CREATE TYPE "ProblemContentType" AS ENUM ('TITLE', 'DESCRIPTION', 'SOLUTION');
-- DropForeignKey
ALTER TABLE "DockerConfig" DROP CONSTRAINT "DockerConfig_language_fkey";
-- DropForeignKey
ALTER TABLE "LanguageServerConfig" DROP CONSTRAINT "LanguageServerConfig_language_fkey";
-- DropForeignKey
ALTER TABLE "Problem" DROP CONSTRAINT "Problem_userId_fkey";
-- DropForeignKey
ALTER TABLE "TestcaseData" DROP CONSTRAINT "TestcaseData_testcaseId_fkey";
-- DropIndex
DROP INDEX "DockerConfig_language_key";
-- DropIndex
DROP INDEX "LanguageServerConfig_language_key";
-- DropIndex
DROP INDEX "Problem_difficulty_idx";
-- DropIndex
DROP INDEX "Problem_userId_idx";
-- AlterTable
ALTER TABLE "DockerConfig" ALTER COLUMN "compileOutputLimit" SET DEFAULT 1048576,
ALTER COLUMN "runOutputLimit" SET DEFAULT 1048576,
DROP COLUMN "language",
ADD COLUMN "language" "Language" NOT NULL,
ADD CONSTRAINT "DockerConfig_pkey" PRIMARY KEY ("language");
-- AlterTable
ALTER TABLE "LanguageServerConfig" DROP COLUMN "language",
ADD COLUMN "language" "Language" NOT NULL,
DROP COLUMN "protocol",
ADD COLUMN "protocol" "Protocol" NOT NULL,
ADD CONSTRAINT "LanguageServerConfig_pkey" PRIMARY KEY ("language");
-- AlterTable
ALTER TABLE "Problem" DROP COLUMN "description",
DROP COLUMN "published",
DROP COLUMN "solution",
DROP COLUMN "title",
ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "isPublished" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL,
ALTER COLUMN "memoryLimit" SET DEFAULT 134217728,
ALTER COLUMN "userId" DROP NOT NULL;
-- AlterTable
ALTER TABLE "Submission" DROP COLUMN "code",
DROP COLUMN "executionTime",
ADD COLUMN "content" TEXT NOT NULL,
ADD COLUMN "timeUsage" INTEGER,
DROP COLUMN "language",
ADD COLUMN "language" "Language" NOT NULL;
-- AlterTable
ALTER TABLE "Template" DROP CONSTRAINT "Template_pkey",
DROP COLUMN "template",
ADD COLUMN "content" TEXT NOT NULL,
DROP COLUMN "language",
ADD COLUMN "language" "Language" NOT NULL,
ADD CONSTRAINT "Template_pkey" PRIMARY KEY ("problemId", "language");
-- AlterTable
ALTER TABLE "Testcase" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL;
-- AlterTable
ALTER TABLE "TestcaseResult" DROP COLUMN "executionTime",
ADD COLUMN "timeUsage" INTEGER,
ALTER COLUMN "output" DROP NOT NULL;
-- AlterTable
ALTER TABLE "User" ALTER COLUMN "email" SET NOT NULL;
-- DropTable
DROP TABLE "EditorLanguageConfig";
-- DropTable
DROP TABLE "TestcaseData";
-- DropEnum
DROP TYPE "EditorLanguage";
-- DropEnum
DROP TYPE "LanguageServerProtocol";
-- CreateTable
CREATE TABLE "ProblemLocalization" (
"problemId" TEXT NOT NULL,
"locale" "Locale" NOT NULL,
"type" "ProblemContentType" NOT NULL,
"content" TEXT NOT NULL,
CONSTRAINT "ProblemLocalization_pkey" PRIMARY KEY ("problemId","locale","type")
);
-- CreateTable
CREATE TABLE "TestcaseInput" (
"id" TEXT NOT NULL,
"index" INTEGER NOT NULL,
"name" TEXT NOT NULL,
"value" TEXT NOT NULL,
"testcaseId" TEXT NOT NULL,
CONSTRAINT "TestcaseInput_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "Problem" ADD CONSTRAINT "Problem_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ProblemLocalization" ADD CONSTRAINT "ProblemLocalization_problemId_fkey" FOREIGN KEY ("problemId") REFERENCES "Problem"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "TestcaseInput" ADD CONSTRAINT "TestcaseInput_testcaseId_fkey" FOREIGN KEY ("testcaseId") REFERENCES "Testcase"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -80,14 +80,18 @@ model Problem {
displayId Int @unique displayId Int @unique
difficulty Difficulty @default(EASY) difficulty Difficulty @default(EASY)
isPublished Boolean @default(false) isPublished Boolean @default(false)
trim Boolean @default(false)
timeLimit Int @default(1000) timeLimit Int @default(1000)
memoryLimit Int @default(134217728) memoryLimit Int @default(134217728)
localizations ProblemLocalization[] localizations ProblemLocalization[]
templates Template[] templates Template[]
testcases Testcase[] testcases Testcase[]
submissions Submission[] submissions Submission[]
userId String? userId String?
user User? @relation(fields: [userId], references: [id], onDelete: SetNull) user User? @relation(fields: [userId], references: [id], onDelete: SetNull)

View File

@ -41,6 +41,7 @@ const problemData: Prisma.ProblemCreateInput[] = [
displayId: 1000, displayId: 1000,
difficulty: "EASY", difficulty: "EASY",
isPublished: true, isPublished: true,
trim: true,
localizations: { localizations: {
create: [ create: [
{ {
@ -734,6 +735,7 @@ int main() {
displayId: 1001, displayId: 1001,
difficulty: "MEDIUM", difficulty: "MEDIUM",
isPublished: true, isPublished: true,
trim: true,
localizations: { localizations: {
create: [ create: [
{ {
@ -1261,6 +1263,7 @@ int main() {
displayId: 1002, displayId: 1002,
difficulty: "HARD", difficulty: "HARD",
isPublished: true, isPublished: true,
trim: true,
localizations: { localizations: {
create: [ create: [
{ {

View File

@ -28,7 +28,8 @@ const startRun = (
joinedInputs: string, joinedInputs: string,
timeLimit: number, timeLimit: number,
memoryLimit: number, memoryLimit: number,
expectedOutput: string expectedOutput: string,
trim: boolean,
): Promise<Status> => { ): Promise<Status> => {
return new Promise<Status>((resolve, reject) => { return new Promise<Status>((resolve, reject) => {
runExec.start({ hijack: true }, async (error, stream) => { runExec.start({ hijack: true }, async (error, stream) => {
@ -71,7 +72,10 @@ const startRun = (
const exitCode = (await runExec.inspect()).ExitCode; const exitCode = (await runExec.inspect()).ExitCode;
const timeUsage = Date.now() - startTime; const timeUsage = Date.now() - startTime;
if (exitCode === 0) { if (exitCode === 0) {
const isCorrect = stdout.trim() === expectedOutput; let isCorrect = stdout === expectedOutput;
if (trim) {
isCorrect = stdout.trim() === expectedOutput.trim();
}
await prisma.testcaseResult.create({ await prisma.testcaseResult.create({
data: { data: {
isCorrect, isCorrect,
@ -131,7 +135,8 @@ const executeRun = async (
submissionId: string, submissionId: string,
timeLimit: number, timeLimit: number,
memoryLimit: number, memoryLimit: number,
testcases: Testcase[] testcases: Testcase[],
trim: boolean
): Promise<Status> => { ): Promise<Status> => {
for (const testcase of testcases) { for (const testcase of testcases) {
const inputs = await prisma.testcaseInput.findMany({ const inputs = await prisma.testcaseInput.findMany({
@ -169,7 +174,8 @@ const executeRun = async (
joinedInputs, joinedInputs,
timeLimit, timeLimit,
memoryLimit, memoryLimit,
testcase.expectedOutput testcase.expectedOutput,
trim
); );
if (status !== Status.RU) { if (status !== Status.RU) {
@ -228,7 +234,7 @@ export const run = async (
testcases: Testcase[] testcases: Testcase[]
): Promise<Status> => { ): Promise<Status> => {
const { runOutputLimit } = config; const { runOutputLimit } = config;
const { timeLimit, memoryLimit } = problem; const { timeLimit, memoryLimit, trim } = problem;
await prisma.submission.update({ await prisma.submission.update({
where: { where: {
@ -248,6 +254,7 @@ export const run = async (
submissionId, submissionId,
timeLimit, timeLimit,
memoryLimit, memoryLimit,
testcases testcases,
trim
); );
}; };