<commit_before><commit_msg>adding var_stack_test back into the code repo<commit_after>#include <stan/agrad/rev/var_stack.hpp>
#include <stan/agrad/rev/operators/operator_multiplication.hpp>
#include <test/unit/agrad/util.hpp>
#include <gtest/gtest.h>

struct foo : public stan::agrad::chainable_alloc {
  std::vector<double> x_;
  foo() : x_(3) { }
  ~foo() { }
  void chain() { }
};

TEST(AgradRev, varStackRecoverNestedSegFaultFix) {
  // this test failed in 2.5, but passes in 2.6
  stan::agrad::start_nested();
  foo* x = new foo();
  x->chain();
  stan::agrad::recover_memory_nested();
  // should be able to do this redundantly:
  stan::agrad::recover_memory(); 
}

// just test basic autodiff;  no more free_memory operation
TEST(AgradRev,varStack) { 
  AVAR a = 2.0;
  AVAR b = -3.0;
  AVAR f = a * b;
  EXPECT_FLOAT_EQ(-6.0,f.val());

  AVEC x = createAVEC(a,b);
  VEC grad_f;
  f.grad(x,grad_f);
  EXPECT_FLOAT_EQ(-3.0,grad_f[0]);
  EXPECT_FLOAT_EQ(2.0,grad_f[1]);

  AVAR aa = 2.0;
  AVAR bb = -3.0;
  AVAR ff = aa * bb;
  EXPECT_FLOAT_EQ(-6.0,ff.val());

  AVEC xx = createAVEC(aa,bb);
  VEC grad_ff;
  ff.grad(xx,grad_ff);
  EXPECT_FLOAT_EQ(-3.0,grad_ff[0]);
  EXPECT_FLOAT_EQ(2.0,grad_ff[1]);
}

TEST(AgradRev, recoverMemoryLogicError) {
  stan::agrad::start_nested();
  EXPECT_THROW(stan::agrad::recover_memory(), std::logic_error);
  stan::agrad::recover_memory_nested(); // clean up for next test
}

TEST(AgradRev, recoverMemoryNestedLogicError) {
  EXPECT_THROW(stan::agrad::recover_memory_nested(), std::logic_error);
}
{'fraction_non_alphanumeric': 0.16514954486345904, 'fraction_numerical': 0.0188556566970091, 'mean_word_length': 4.721189591078067, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4881123', 'n_tokens_mistral': 648, 'n_tokens_neox': 634, 'n_words': 113}