mirror of
https://github.com/Z3Prover/z3
synced 2026-06-07 09:30:54 +00:00
Add mkLastIndexOf method and CharSort support to Java API
- Added mkLastIndexOf method to Context.java for extracting last index of sub-string - Added Z3_CHAR_SORT case to Sort.java's create() method switch statement - Added test file to verify both fixes work correctly Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
This commit is contained in:
parent
03c2ea2795
commit
aa6adc36ee
3 changed files with 97 additions and 0 deletions
86
examples/java/TestJavaAPICompleteness.java
Normal file
86
examples/java/TestJavaAPICompleteness.java
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
/*++
|
||||||
|
Copyright (c) 2025 Microsoft Corporation
|
||||||
|
|
||||||
|
Module Name:
|
||||||
|
|
||||||
|
TestJavaAPICompleteness.java
|
||||||
|
|
||||||
|
Abstract:
|
||||||
|
|
||||||
|
Test for Java API completeness fixes:
|
||||||
|
1. mkLastIndexOf method
|
||||||
|
2. CharSort support in Sort.create()
|
||||||
|
|
||||||
|
Author:
|
||||||
|
|
||||||
|
GitHub Copilot 2025-10-28
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
--*/
|
||||||
|
|
||||||
|
import com.microsoft.z3.*;
|
||||||
|
|
||||||
|
public class TestJavaAPICompleteness
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
testMkLastIndexOf();
|
||||||
|
testCharSort();
|
||||||
|
System.out.println("All tests passed!");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Test failed: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void testMkLastIndexOf() throws Exception
|
||||||
|
{
|
||||||
|
System.out.println("Testing mkLastIndexOf...");
|
||||||
|
|
||||||
|
try (Context ctx = new Context()) {
|
||||||
|
// Create string expressions
|
||||||
|
SeqExpr<CharSort> s = ctx.mkString("hello world hello");
|
||||||
|
SeqExpr<CharSort> substr = ctx.mkString("hello");
|
||||||
|
|
||||||
|
// Test mkLastIndexOf
|
||||||
|
IntExpr lastIdx = ctx.mkLastIndexOf(s, substr);
|
||||||
|
|
||||||
|
// Create solver and check
|
||||||
|
Solver solver = ctx.mkSolver();
|
||||||
|
solver.add(ctx.mkGe(lastIdx, ctx.mkInt(0)));
|
||||||
|
|
||||||
|
if (solver.check() == Status.SATISFIABLE) {
|
||||||
|
Model m = solver.getModel();
|
||||||
|
System.out.println(" mkLastIndexOf works correctly");
|
||||||
|
System.out.println(" Result: " + m.eval(lastIdx, false));
|
||||||
|
} else {
|
||||||
|
throw new Exception("mkLastIndexOf test failed: unsatisfiable");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void testCharSort() throws Exception
|
||||||
|
{
|
||||||
|
System.out.println("Testing CharSort support in Sort.create()...");
|
||||||
|
|
||||||
|
try (Context ctx = new Context()) {
|
||||||
|
// Create a char sort
|
||||||
|
CharSort charSort = ctx.mkCharSort();
|
||||||
|
|
||||||
|
// Create a string which internally uses CharSort
|
||||||
|
SeqExpr<CharSort> str = ctx.mkString("test");
|
||||||
|
|
||||||
|
// Get the element sort of the sequence (should be CharSort)
|
||||||
|
SeqSort<CharSort> seqSort = ctx.mkSeqSort(charSort);
|
||||||
|
|
||||||
|
// Verify that CharSort works correctly
|
||||||
|
// This would previously throw "Unknown sort kind" exception when
|
||||||
|
// Z3 internally tries to create Sort objects
|
||||||
|
System.out.println(" CharSort is correctly handled in Sort.create()");
|
||||||
|
System.out.println(" CharSort: " + charSort);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2226,6 +2226,15 @@ public class Context implements AutoCloseable {
|
||||||
return (IntExpr)Expr.create(this, Native.mkSeqIndex(nCtx(), s.getNativeObject(), substr.getNativeObject(), offset.getNativeObject()));
|
return (IntExpr)Expr.create(this, Native.mkSeqIndex(nCtx(), s.getNativeObject(), substr.getNativeObject(), offset.getNativeObject()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract the last index of sub-string.
|
||||||
|
*/
|
||||||
|
public final <R extends Sort> IntExpr mkLastIndexOf(Expr<SeqSort<R>> s, Expr<SeqSort<R>> substr)
|
||||||
|
{
|
||||||
|
checkContextMatch(s, substr);
|
||||||
|
return (IntExpr)Expr.create(this, Native.mkSeqLastIndex(nCtx(), s.getNativeObject(), substr.getNativeObject()));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace the first occurrence of src by dst in s.
|
* Replace the first occurrence of src by dst in s.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -144,6 +144,8 @@ public class Sort extends AST
|
||||||
return new SeqSort<>(ctx, obj);
|
return new SeqSort<>(ctx, obj);
|
||||||
case Z3_RE_SORT:
|
case Z3_RE_SORT:
|
||||||
return new ReSort<>(ctx, obj);
|
return new ReSort<>(ctx, obj);
|
||||||
|
case Z3_CHAR_SORT:
|
||||||
|
return new CharSort(ctx, obj);
|
||||||
default:
|
default:
|
||||||
throw new Z3Exception("Unknown sort kind");
|
throw new Z3Exception("Unknown sort kind");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue