Skip to content

Commit

Permalink
v3.0 process tests (#1153)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksyeo1010 authored Nov 2, 2023
1 parent 2f4fa21 commit 48c27d3
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 3 deletions.
29 changes: 29 additions & 0 deletions binding/dotnet/PorcupineTest/MainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,35 @@ public void TestMessageStack()
}
}

[TestMethod]
public void TestProcessMessageStack()
{
List<BuiltInKeyword> keywords = new List<Pv.BuiltInKeyword>() { BuiltInKeyword.PORCUPINE };
Porcupine p = Porcupine.FromBuiltInKeywords(
_accessKey,
keywords,
GetModelPath("en"));
short[] testPcm = new short[p.FrameLength];

var obj = typeof(Porcupine).GetField("_libraryPointer", BindingFlags.NonPublic | BindingFlags.Instance);
IntPtr address = (IntPtr)obj.GetValue(p);
obj.SetValue(p, IntPtr.Zero);

try
{
int res = p.Process(testPcm);
Assert.IsTrue(res == 100);
}
catch (PorcupineException e)
{
Assert.IsTrue(0 < e.MessageStack.Length);
Assert.IsTrue(e.MessageStack.Length < 8);
}

obj.SetValue(p, address);
p.Dispose();
}

private List<short> GetPcmFromFile(string audioFilePath, int expectedSampleRate)
{
List<short> data = new List<short>();
Expand Down
30 changes: 30 additions & 0 deletions binding/go/porcupine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,33 @@ func TestMessageStack(t *testing.T) {
t.Fatalf("length of 1st init '%d' does not match 2nd init '%d'", len(err.Error()), len(err2.Error()))
}
}

func TestProcessMessageStack(t *testing.T) {
language := "en"
keywords := []string{"porcupine"}
porcupine = Porcupine{
AccessKey: testAccessKey,
ModelPath: getTestModelPath(language),
KeywordPaths: getTestKeywordPaths(language, keywords)}

err := porcupine.Init()
if err != nil {
t.Fatalf("%v", err)
}

address := porcupine.handle
porcupine.handle = nil

testPcm := make([]int16, FrameLength)

_, err = porcupine.Process(testPcm)
porcupine.handle = address
if err == nil {
t.Fatalf("Expected porcupine process to fail")
}

delErr := porcupine.Delete()
if delErr != nil {
t.Fatalf("%v", delErr)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,19 @@ class PorcupineAppTestUITests: BaseTest {
XCTAssert("\(error.localizedDescription)".count == first_error.count)
}
}

func testProcessMessageStack() throws {
let p = try Porcupine.init(accessKey: accessKey, keyword: Porcupine.BuiltInKeyword.porcupine)
p.delete()

var testPcm: [Int16] = []
testPcm.reserveCapacity(Int(Porcupine.frameLength))

do {
let res = try p.process(pcm: testPcm)
XCTAssert(res == 100)
} catch {
XCTAssert("\(error.localizedDescription)".count > 0)
}
}
}
23 changes: 23 additions & 0 deletions binding/python/test_porcupine.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,29 @@ def test_message_stack(self):
self.assertEqual(len(error), len(e.message_stack))
self.assertListEqual(list(error), list(e.message_stack))

def test_process_message_stack(self):
relative_path = '../..'

p = Porcupine(
access_key=sys.argv[1],
library_path=pv_library_path(relative_path),
model_path=get_model_path_by_language(relative_path, 'en'),
keyword_paths=get_keyword_paths_by_language(relative_path, 'en', ['porcupine']),
sensitivities=[0.5])
test_pcm = [0] * p.frame_length

address = p._handle
p._handle = None

try:
res = p.process(test_pcm)
self.assertEqual(res, 100)
except PorcupineError as e:
self.assertGreater(len(e.message_stack), 0)
self.assertLess(len(e.message_stack), 8)

p._handle = address


if __name__ == '__main__':
if len(sys.argv) != 2:
Expand Down
41 changes: 41 additions & 0 deletions binding/rust/src/porcupine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,44 @@ impl Drop for PorcupineInner {
}
}
}

#[cfg(test)]
mod tests {
use std::env;
use std::path::PathBuf;

use crate::util::{pv_keyword_paths, pv_library_path, pv_model_path};
use crate::porcupine::{BuiltinKeywords, PorcupineInner};

#[test]
fn test_process_error_stack() {
let access_key = env::var("PV_ACCESS_KEY")
.expect("Pass the AccessKey in using the PV_ACCESS_KEY env variable");

let default_keyword_paths = pv_keyword_paths();
let keyword_path = default_keyword_paths.get(BuiltinKeywords::Porcupine.to_str())
.expect("Unable to find keyword file for specified keyword");

let mut inner = PorcupineInner::init(
&access_key.as_str(),
pv_library_path(),
pv_model_path(),
&[PathBuf::from(keyword_path)],
&[0.5],
).expect("Unable to create Porcupine");

let test_pcm = vec![0; inner.frame_length as usize];
let address = inner.cporcupine;
inner.cporcupine = std::ptr::null_mut();

let res = inner.process(&test_pcm);

inner.cporcupine = address;
if let Err(err) = res {
assert!(err.message_stack.len() > 0);
assert!(err.message_stack.len() < 8);
} else {
assert!(res.unwrap() == 100);
}
}
}
2 changes: 1 addition & 1 deletion binding/unity/Assets/Porcupine/PorcupineException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public PorcupineException(string message, string[] messageStack) : base(ModifyMe
this._messageStack = messageStack;
}

public string[] messageStack
public string[] MessageStack
{
get => _messageStack;
}
Expand Down
34 changes: 32 additions & 2 deletions binding/unity/Assets/Porcupine/Tests/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void TestMessageStack()
}
catch (PorcupineException e)
{
messageList = e.messageStack;
messageList = e.MessageStack;
}

Assert.IsTrue(0 < messageList.Length);
Expand All @@ -258,11 +258,41 @@ public void TestMessageStack()
{
for (int i = 0; i < messageList.Length; i++)
{
Assert.AreEqual(messageList[i], e.messageStack[i]);
Assert.AreEqual(messageList[i], e.MessageStack[i]);
}
}
}

[Test]
public void TestProcessMessageStack()
{
List<Porcupine.BuiltInKeyword> keywords = new List<Porcupine.BuiltInKeyword>() { Porcupine.BuiltInKeyword.PORCUPINE };
Porcupine p = Porcupine.FromBuiltInKeywords(
ACCESS_KEY,
keywords,
GetModelPath("en"));

short[] testPcm = new short[p.FrameLength];

var obj = typeof(Porcupine).GetField("_libraryPointer", BindingFlags.NonPublic | BindingFlags.Instance);
IntPtr address = (IntPtr)obj.GetValue(p);
obj.SetValue(p, IntPtr.Zero);

try
{
int res = p.Process(testPcm);
Assert.IsTrue(res == 100);
}
catch (PorcupineException e)
{
Assert.IsTrue(0 < e.MessageStack.Length);
Assert.IsTrue(e.MessageStack.Length < 8);
}

obj.SetValue(p, address);
p.Dispose();
}

[Test]
public void SingleKeyword([ValueSource("SingleKeywordTestData")] SingleKeywordTest testCase)
{
Expand Down
Binary file modified binding/unity/porcupine-3.0.0.unitypackage
Binary file not shown.

0 comments on commit 48c27d3

Please sign in to comment.