void VerifyLex(const wstring& refToLex, const TokenVector& expectedTokens, bool shouldSucceed /*= true*/) { wchar_t const* first = refToLex.c_str(); wchar_t const* last = first + refToLex.size(); TokenVector actualTokens; auto cur = g_lexer.begin(first, last); auto end = g_lexer.end(); for(; cur != end && cur->is_valid() ; ++cur) { try { actualTokens.push_back(make_pair(cur->id(), wstring(cur->value().begin(), cur->value().end()))); } catch(const std::bad_alloc&) { break; } } const bool lexingSucceeded = (cur == end); const bool lexResultAsExpected = lexingSucceeded == shouldSucceed; //Kind of goofy to have to do it this way but the unit test stuff is not set up to output wstrings :( if(!lexResultAsExpected) { wcout << endl << L"Lexing was expected to " << (shouldSucceed ? L"pass" : L"fail") << L"but it " << (shouldSucceed ? L"failed" : L"passed") << L" with input '" << refToLex << L"'" << endl; } BOOST_CHECK( lexResultAsExpected ); if(shouldSucceed) { const bool sizeMatches = actualTokens.size() == expectedTokens.size(); if(!sizeMatches) { wcout << endl << L"Expected lexing to result in " << expectedTokens.size() << L" but it actually resulted in " << actualTokens.size() << L" tokens." << endl; } BOOST_CHECK( sizeMatches ); //avoid annoying signed/unsigned mismatch or relying on the fact that ::size_type is == size_t typedef decltype(actualTokens.size()) IndexType; for(IndexType i = 0 ; i < actualTokens.size() ; ++i) { auto actual = actualTokens[i]; auto expected = expectedTokens[i]; const bool areEqual = (actual == expected); if(!areEqual) { wcout << endl << L"Expected token '" << expected.second + L"' (" << expected.first << "), but got '" << actual.second << L"' (" << actual.first << L") at token index " << i << endl; } BOOST_CHECK( areEqual ); } } }
float similarity(const TokenVector& lv, const TokenVector& rv) { if (lv.size() != rv.size()) { return -1; } float sim = 0.0; float lsum = 0.0; float rsum = 0.0; for (std::size_t i = 0; i < lv.size(); ++i) { sim += lv[i] * rv[i]; lsum += lv[i]; rsum += rv[i]; } return sim / lsum/ rsum; }
void PSU1Rules::Detect(const TokenVector& tv) { for(unsigned int i=0; i<tv.size(); ++i) { gKey.Process(tv[i]); t1Key.Process(tv[i]); t2Key.Process(tv[i]); } const double bauda = any_cast<double>(param::FindParameter(t1Key.GetTupleRef(), "bauda").value); for(unsigned int i=0; i<tv.size(); ++i) { trKey.Process(tv[i],bauda); txaKey.Process(tv[i],bauda); codeKey.Process(tv[i]); saKey.Process(tv[i]); } }
// 逆波兰式计算求值 bool CalcExpr(const TokenVector & input, const TokenVector & expr, Token & result) { TokenStack stack; TokenVector::const_iterator it = expr.begin(); for (; it != expr.end(); ++it) { if (it->type == INT || it->type == FLOAT || it->type == BOOL) { stack.push(*it); } else if (it->type == ARG) { if (it->value.index >= input.size()) return false; const Token & a = input[it->value.index]; if (a.type == INT || a.type == FLOAT || a.type == BOOL) { stack.push(a); } else { return false; } } else if (it->type == RAND) { Token a; a.type = INT; a.value.i = rand(); stack.push(a); } else if (it->type == OP) { if (stack.size() < 2) return false; Token a = stack.top(); stack.pop(); Token b = stack.top(); stack.pop(); Token c; if (GetValue(it->value.op, b, a, c)) { stack.push(c); } else { return false; } } } if (!stack.empty()) { result = stack.top(); return true; } else { return false; } }